-
Notifications
You must be signed in to change notification settings - Fork 0
111. Minimum Depth of Binary Tree #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,59 @@ | ||
| # Step 1 | ||
|
|
||
| ## Recursive DFS (失敗) | ||
|
|
||
| - nodeがNoneだったら0を返す | ||
| - 左右最小の深さに1を足して返す | ||
|
|
||
| ```python | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
| return 1 + min(self.minDepth(root.left), self.minDepth(root.right)) | ||
| ``` | ||
|
|
||
| - 葉が片方にしかないとき、rootに0が返ってくる | ||
|
|
||
| 時間計算量: O(n) | ||
|
|
||
| 空間計算量: O(n) | ||
|
|
||
| ## Iterative DFS | ||
|
|
||
| - nodeはNoneではないが、left, rightがNoneの場合に最小の深さを更新する | ||
|
|
||
| ```python | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| res = 10 ** 5 | ||
| stack = [[root, 1]] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 変数名にはどんなものが入っているかを示したほうが分かりやすいかなと思います。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stackの変数名どうするか悩みました。 変数の中身が何かわかりやすくする場合、node_and_depthが良いと思います。他方で今回の処理がDFSであることをわかりやすくするためにはstackにしておいた方が良いとも思いました。 変数の中身のわかりやすさか処理のわかりやすさ(今回の場合DFSをしているとわかりやすく示すこと)のどちらを優先するかだと思います。 |
||
| while stack: | ||
| node, depth = stack.pop() | ||
| if node: | ||
| if node.left is None and node.right is None: | ||
| res = min(res, depth) | ||
| else: | ||
| stack.append([node.left, depth + 1]) | ||
| stack.append([node.right, depth + 1]) | ||
| return res | ||
| ``` | ||
| 時間計算量: | ||
|
|
||
| 空間計算量: | ||
| 時間計算量: O(n) | ||
|
|
||
| 空間計算量: O(n) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,26 @@ | ||
| # Step 2 | ||
|
|
||
| ```python | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| min_depth = float("inf") | ||
| stack = [(root, 1)] | ||
| while stack: | ||
| node, depth = stack.pop() | ||
| if node: | ||
| if node.left is None and node.right is None: | ||
| min_depth = min(res, depth) | ||
| else: | ||
| stack.append((node.left, depth + 1)) | ||
| stack.append((node.right, depth + 1)) | ||
| return min_depth | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,30 @@ | ||
| # Step 3 | ||
|
|
||
| ```python | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
| min_depth = float("inf") | ||
| stack = [(root, 1)] | ||
| while stack: | ||
| node, depth = stack.pop() | ||
| if node: | ||
| if node.left is None and node.right is None: | ||
| min_depth = min(min_depth, depth) | ||
| else: | ||
| stack.append((node.left, depth + 1)) | ||
| stack.append((node.right, depth + 1)) | ||
| return min_depth | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 今回の問題はrootから最も近い葉までの距離を求める問題ですので、 |
||
| ``` | ||
| 1回目: 分 秒 | ||
| 1回目: 2分 11秒 | ||
|
|
||
| 2回目: 分 秒 | ||
| 2回目: 2分 6秒 | ||
|
|
||
| 3回目: 分 秒 | ||
| 3回目: 2分 8秒 | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resの値は、将来変更される可能性が高いと思います。
どんな変数なのか、コメントアウトしておくと親切かと思います。