-
Notifications
You must be signed in to change notification settings - Fork 0
104. Maximum Depth of Binary Tree #21
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
Conversation
# self.right = right | ||
class Solution: | ||
def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
stack = [(root, 1)] |
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.
nodes_and_depthsなどとしてもいい気がします
stack.append((node.right, depth + 1)) | ||
|
||
return max_depth | ||
No newline at end of file |
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.
特に問題ないと思います。
再帰への書き換え、queueを使う/使わないBFS等の実装もありました
|
||
* https://github.com/nittoco/leetcode/pull/14/files | ||
* https://discord.com/channels/1084280443945353267/1227073733844406343/1236235351140339742 | ||
* 私の方法は上から配る方だと思うが、概念として下から戻す方法が存在するのはわかるものの、今回どうやってやるのかよくわからない。このへん読んでみたが、結局なにをどうしているのかよくわからん…。一旦次に進む。 |
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.
先に進んでもいいかもしれません。一応、こちらに(別の問題ですが) Python のバージョンがあるので、これをみたらやろうとしていること分かるかもしれません。
potrue/leetcode#22 (comment)
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.
一応機械的に namedtuple で書き直してみましたが、結局あまりよくわかりませんでした。一旦先に進んで、どこかで戻ってみようかと思います… (return_value, left_value, right_value が何なのかすらわかっていない)。
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
StackEntry = namedtuple('StackEntry', ['node', 'depth', 'return_value', 'left_value', 'right_value'])
result = []
stack = [StackEntry(root, 1, result, [], [])]
while stack:
node, depth, return_value, left_value, right_value = stack[-1]
if node.left is not None and not left_value:
stack.append(StackEntry(node.left, depth + 1, left_value, [], []))
continue
if node.right is not None and not right_value:
stack.append(StackEntry(node.right, depth + 1, right_value, [], []))
continue
if not left_value and not right_value:
min_depth = depth
else:
min_depth = min(value[0] for value in (left_value, right_value) if value)
return_value.append(min_depth)
stack.pop()
return result[0]
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.
ここでの議論が参考になりそうなので記録しておきます。
https://github.com/ryosuketc/leetcode_arai60/pull/23/files#r2122113958
104. Maximum Depth of Binary Tree
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/