-
Notifications
You must be signed in to change notification settings - Fork 0
Maximum depth of binary #34
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: arai60
Are you sure you want to change the base?
Conversation
| while stack: | ||
| current_node, depth_so_far = stack.pop() | ||
| if current_node is None: | ||
| continue | ||
| if max_depth < depth_so_far: | ||
| max_depth = depth_so_far | ||
| stack.append((current_node.right, depth_so_far + 1)) | ||
| stack.append((current_node.left, depth_so_far + 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.
自分はそれぞれif current_node.right とif current_node,leftがNoneでないならappend、と書いてましたが、Takahashiさんのcurrent_nodeがNoneなら更新しない、という方が簡潔でいいですね
|
特に気になるところはなかったです。 |
fhiyo
left a 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.
良いと思います!
| @@ -0,0 +1,50 @@ | |||
| """ | |||
| Reference | |||
| fhiyo: https://github.com/fhiyo/leetcode/pull/23/files phase1の段階で別の関数挟まずに書き終えてて凄すぎてびっくりしてしまった。関数プログラミングっぽい書き方 | |||
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.
Arai60は最近一度解いている状態でやってるので、覚えているものはできるんです... (^_^;)
| max_depth = 0 | ||
| stack = [(root, 1)] | ||
| while stack: | ||
| current_node, depth_so_far = stack.pop() |
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.
depth_so_farって「これまでの深さ」みたいな意味になりますか? (英語は自信ないです)
単なるノードの深さなのでdepthで良いような気がしました。
| if max_depth < depth: | ||
| max_depth = 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.
行きがけでmax_depthを更新する場合、leafに到達したときだけ更新してもいいかもしれません。
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.
確かにそちらの方が効率がよさそうです
問題
104. Maximum Depth of Binary Tree