Skip to content

104. Maximum Depth of Binary Tree#23

Open
fhiyo wants to merge 1 commit intomainfrom
104_maximum-depth-of-binary-tree
Open

104. Maximum Depth of Binary Tree#23
fhiyo wants to merge 1 commit intomainfrom
104_maximum-depth-of-binary-tree

Conversation

@fhiyo
Copy link
Copy Markdown
Owner

@fhiyo fhiyo commented Jun 18, 2024

if root is None:
return 0
depth = 0
nodes = [root]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nodes = []
append_if_exists(node, nodes)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

optional: 上のように書いてもいいですね。

```py
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
def append_if_exists(node: Optional[TreeNode], nodes: list[TreeNode]):
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://docs.python.org/3/library/heapq.html#heapq.heappush

- 空間計算量: O(h)

```py
class Solution:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これの再帰版もありますね。

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.

なるべく再現するとこんな感じですかね。

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        max_depth = 0

        def max_depth_helper(node: Optional[TreeNode], depth: int):
            nonlocal max_depth
            max_depth = max(max_depth, depth)
            if node.left is not None:
                max_depth_helper(node.left, depth + 1)
            if node.right is not None:
                max_depth_helper(node.right, depth + 1)

        max_depth_helper(root, 1)
        return max_depth

少し整理してみたらこうなりました。

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        max_depth = 0

        def max_depth_helper(node: Optional[TreeNode], depth: int):
            nonlocal max_depth
            max_depth = max(max_depth, depth)
            if node is None:
                return
            max_depth_helper(node.left, depth + 1)
            max_depth_helper(node.right, depth + 1)

        max_depth_helper(root, 0)
        return max_depth

return
nodes.append(node)

if root is None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

一応、これなくても動きますかね。まあ、書いておいてもいいですよね。

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.

や、問題設定上 The number of nodes in the tree is in the range [0, 10^4]. なので、入力でrootがNoneな場合があり得ますね (そして node.left のところでエラーになる)

- https://discord.com/channels/1084280443945353267/1227073733844406343/1236232864098684928
- https://discord.com/channels/1084280443945353267/1227073733844406343/1236695050902048899

帰りがけの処理を行う再帰をスタックで実装したバージョン。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的にやや複雑な処理だなと感じたんですが、割とサラッと実装できる感じでしょうか。

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.

あまり覚えてないんですが、そこまでさらっとは出来なかった気がします...

if root is None:
return 0
depth = 0
nodes = [root]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には deque に (node, depth) を入れ、先頭から取り出し、末尾に入れていくタイプの実装のほうが好きですが、fhiyo さんの実装でもよいと思います。

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