Skip to content

617. Merge Two Binary Trees#25

Open
fhiyo wants to merge 1 commit intomainfrom
617_merge-two-binary-trees
Open

617. Merge Two Binary Trees#25
fhiyo wants to merge 1 commit intomainfrom
617_merge-two-binary-trees

Conversation

@fhiyo
Copy link
Copy Markdown
Owner

@fhiyo fhiyo commented Jun 23, 2024


n: 2つの木でoverlapしているノードの数, h: 根から2つの木でoverlapしている部分までの高さ
- 時間計算量: O(n)
- 空間計算量: O(h)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nits]
高さ分だけスタックメモリに積まれるのでO(h)にする気持ちはわかります。一点だけ、計算量を出す際に、最悪の場合の計算量を取ることが多いです。最悪の場合、空間計算量はどうなるんでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あら、最悪計算量を考えていたつもりでした。最悪だとどうなるんでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

例えば、以下のような二つの木をマージする場合、空間計算量はO(n)になると思います。この場合、空間計算量も O(n)になります。O(h)として表してもいいのですが、最悪の場合をわかりやすく示した方が良いかと思います。

Tree1
    1
     \
      2
       \
        3
         \
          ...
           \
            n
Tree2
    1
     \
      2
       \
        3
         \
          ...
           \
            n

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、ありがとうございます。
うーんただこれだと、正直高さに比例するという方が正確な気はしてしまいます。
最悪の場合でもn==hという話なのでO(h)で間違いはないですし、どちらがわかりやすいのか自分には判断できませんでした...
もちろんO(n)でも間違いではないので問題はないと思いますが。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

面接官次第だと思いますので、DFSで不完全な木を巡回する場合の空間計算量にはこいう表し方もあることを覚えて、面接の際に即答できればいいと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

どちらでも大丈夫だと思います。hの範囲がlog NからNになりますね。

return merged_root
nodes = [(root1, root2, merged_root)]
while nodes:
node1, node2, merged_node = nodes.pop()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好みの問題ですが、自分はBFSにするため、nodesはdequeにして、popleft()にすると思いました。

return node2
if not node2:
return node1
node = TreeNode(node1.val + node2.val)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

新しいnodeオブジェクトを作成せずとも、node1にマージしていく方法でも良いかと思いました。node1.val += node2.val

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

新しいのを作るか作らないのか、古い入力を壊すのか壊さないのか、共有するのかしないのか(変更しない前提ならばメモリー使用量が減る)、などのオプションがあって、自分がどれを「選択」したかを意識しましょう。

入力を破壊している。
https://discord.com/channels/1084280443945353267/1252267683731345438/1252556045524537344
出力を変更されるとキャッシュが変わって次からの呼び出しが狂う。
https://discord.com/channels/1084280443945353267/1252267683731345438/1252591437485441024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants