Open
Conversation
nittoco
reviewed
Jun 28, 2024
Comment on lines
+41
to
+48
| 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.
自分はそれぞれif current_node.right とif current_node,leftがNoneでないならappend、と書いてましたが、Takahashiさんのcurrent_nodeがNoneなら更新しない、という方が簡潔でいいですね
|
特に気になるところはなかったです。 |
fhiyo
reviewed
Jun 28, 2024
| @@ -0,0 +1,50 @@ | |||
| """ | |||
| Reference | |||
| fhiyo: https://github.com/fhiyo/leetcode/pull/23/files phase1の段階で別の関数挟まずに書き終えてて凄すぎてびっくりしてしまった。関数プログラミングっぽい書き方 | |||
There was a problem hiding this comment.
Arai60は最近一度解いている状態でやってるので、覚えているものはできるんです... (^_^;)
| max_depth = 0 | ||
| stack = [(root, 1)] | ||
| while stack: | ||
| current_node, depth_so_far = stack.pop() |
There was a problem hiding this comment.
depth_so_farって「これまでの深さ」みたいな意味になりますか? (英語は自信ないです)
単なるノードの深さなのでdepthで良いような気がしました。
kazukiii
reviewed
Jun 29, 2024
Comment on lines
+10
to
+11
| if max_depth < depth: | ||
| max_depth = depth |
There was a problem hiding this comment.
行きがけでmax_depthを更新する場合、leafに到達したときだけ更新してもいいかもしれません。
Owner
Author
There was a problem hiding this comment.
確かにそちらの方が効率がよさそうです
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題
104. Maximum Depth of Binary Tree