-
Notifications
You must be signed in to change notification settings - Fork 0
【Arai60】22問目 111_Minimum Depth of Binary Tree #22
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
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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # BFS | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| node_queue = deque([(root, 0)]) | ||
| while node_queue: | ||
| node, depth = node_queue.popleft() | ||
| if not node.left and not node.right: | ||
| break | ||
| if node.left: | ||
| node_queue.append((node.left, depth + 1)) | ||
| if node.right: | ||
| node_queue.append((node.right, depth + 1)) | ||
| min_node_num = depth + 1 | ||
| return min_node_num | ||
|
|
||
|
|
||
| # DFS | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| def search_min_depth(node): | ||
|
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. 好みの問題なのですが、 minDepth() をそのまま使って再帰しても良いと思いました。
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. minDepth() をそのまま使い、少しコードをすっきりさせることができました。 |
||
| if not node.left and not node.right: | ||
| return 0 | ||
| left_depth = float(inf) | ||
|
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. depth を求めようとしているにもかかわらず、 float を使用している点に違和感を感じました。 あたりでいかがでしょうか?
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. [2,null,3,null,4,null,5,null,6] のパターンで 1を出力するとWrong Answerになってしまいます。 |
||
| right_depth = float(inf) | ||
| if node.left: | ||
| left_depth = search_min_depth(node.left) + 1 | ||
| if node.right: | ||
| right_depth = search_min_depth(node.right) + 1 | ||
| min_depth = min(left_depth, right_depth) | ||
| return min_depth | ||
|
|
||
| if not root: | ||
| return 0 | ||
| min_node_num = search_min_depth(root) + 1 | ||
| return min_node_num | ||
|
|
||
|
|
||
| # DFSをstackで実装 | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| node_depth_stack = [(root, 0)] | ||
| min_depth = float(inf) | ||
| while node_depth_stack: | ||
| node, depth = node_depth_stack.pop() | ||
| if not node.left and not node.right: | ||
| min_depth = min(min_depth, depth) | ||
| continue | ||
| if node.right: | ||
| node_depth_stack.append((node.right, depth + 1)) | ||
| if node.left: | ||
| node_depth_stack.append((node.left, depth + 1)) | ||
| min_node_num = min_depth + 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.
|
||
| return min_node_num | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # BFS | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| node_depth_queue = deque([(root, 0)]) | ||
| while node_depth_queue: | ||
| node, depth = node_depth_queue.popleft() | ||
| if not node.left and not node.right: | ||
| return depth + 1 | ||
| if node.left: | ||
| node_depth_queue.append((node.left, depth + 1)) | ||
| if node.right: | ||
| node_depth_queue.append((node.right, depth + 1)) | ||
|
|
||
|
|
||
| # DFS | ||
| # left_depthとright_depthをmin_depthに統合 | ||
| # min_depthの初期値をintの最大値に変更 | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| if not root.left and not root.right: | ||
| return 1 | ||
| min_depth = sys.maxsize | ||
| if root.left: | ||
| min_depth = min(min_depth, self.minDepth(root.left) + 1) | ||
| if root.right: | ||
| min_depth = min(min_depth, self.minDepth(root.right) + 1) | ||
| return min_depth | ||
|
|
||
|
|
||
| # DFSをstackで実装 | ||
| # min_depthの初期値をintの最大値に変更 | ||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| node_depth_stack = [(root, 0)] | ||
| min_depth = sys.maxsize | ||
| while node_depth_stack: | ||
| node, depth = node_depth_stack.pop() | ||
| if not node.left and not node.right: | ||
| min_depth = min(min_depth, depth) | ||
| continue | ||
| if node.right: | ||
| node_depth_stack.append((node.right, depth + 1)) | ||
| if node.left: | ||
| node_depth_stack.append((node.left, depth + 1)) | ||
| return min_depth + 1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
|
|
||
| class Solution: | ||
| def minDepth(self, root: Optional[TreeNode]) -> int: | ||
| if not root: | ||
| return 0 | ||
| node_depth_queue = deque([(root, 0)]) | ||
| while node_depth_queue: | ||
| node, depth = node_depth_queue.popleft() | ||
| if not node.left and not node.right: | ||
| break | ||
| if node.left: | ||
| node_depth_queue.append((node.left, depth + 1)) | ||
| if node.right: | ||
| node_depth_queue.append((node.right, depth + 1)) | ||
| min_node_num = depth + 1 | ||
| return min_node_num |
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.
あくまで C++ をメインで使っている人間の印象なのですが、ループの中で定義した変数をループの外で参照している点に違和感を感じました。 break するのではなく、直接 return depth + 1 してしまったほうが、個人的には直感的に思いました。
また、 min_node_num というフレーズですと、 the number of nodes along the shortest path の部分が伝わらないように思いました。関数名と被ってやや微妙ですが、 min_depth でも良いように思いました。
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.
ループ内で定義した変数は、ループ内のローカル変数といったような意識で使ってみます。
意識したことなかったので、気にしてみます。