-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# 104. Maximum Depth of Binary Tree | ||
|
||
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/ | ||
|
||
## Comments | ||
|
||
### step1 | ||
|
||
* 最初 `SolutionAC` の方で書いていたんだけど、これだと、(None, 1) のような要素が append されるので、なんか None に対して depth がついているの嫌な気がした。ので、`SolutionRuntimeError` に書き直した。 | ||
* よく考えると、root が None のケースを考慮していなくて Runtime Error。その後すぐに `SolutionAC` に書き直した。4:50 くらい。 | ||
* 大雑把に DFS、BFS を考えていて、再帰で書こうかと思ったのだが、再帰で書くときに depth の引数渡すのがなんとなく面倒だったのと、10^4 の制約でデフォルトの recursion limit 超える気がしたので iterative DFS にした。 | ||
* > The number of nodes in the tree is in the range [0, 104]. | ||
|
||
### step2 | ||
|
||
* https://github.com/nittoco/leetcode/pull/14/files | ||
* https://discord.com/channels/1084280443945353267/1227073733844406343/1236235351140339742 | ||
* 私の方法は上から配る方だと思うが、概念として下から戻す方法が存在するのはわかるものの、今回どうやってやるのかよくわからない。このへん読んでみたが、結局なにをどうしているのかよくわからん…。一旦次に進む。 | ||
* https://discord.com/channels/1084280443945353267/1227073733844406343/1236695050902048899 | ||
* というかそもそも下から戻す再帰だいぶ苦手かも。 | ||
* > 訪れているかの判定を次の再帰に任せた場合、やや処理速度が落ちる点は気にしたほうが良いと思います。理由は、関数の呼び出しが、一般的に、コストがかかる処理だからです。個人的には再帰関数呼び出しの前に判定したほうが良いと思います。 | ||
* https://github.com/sakupan102/arai60-practice/pull/22#discussion_r1590605423 | ||
* これだと、`SolutionRuntimeError` の冒頭に `if root is None` を足せば動く | ||
* 感覚として、個人的には再帰で書くより stack で iterative に書くほうが好きらしい。 | ||
|
||
### step3 | ||
|
||
* step2 で、これといっては直したいところもなかったので、一旦何回か書いてみて馴染まないところがあるか考えることにした。 | ||
* 1:10 -> 1:10 (特に変化がないので打ち切り) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class SolutionRuntimeError: | ||
def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
max_depth = 0 | ||
stack = [(root, 1)] | ||
while stack: | ||
node, depth = stack.pop() | ||
max_depth = max(max_depth, depth) | ||
if node.left is not None: | ||
stack.append((node.left, depth + 1)) | ||
if node.right is not None: | ||
stack.append((node.right, depth + 1)) | ||
|
||
return max_depth | ||
|
||
|
||
class SolutionAC: | ||
def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
max_depth = 0 | ||
stack = [(root, 1)] | ||
while stack: | ||
node, depth = stack.pop() | ||
if node is None: | ||
continue | ||
max_depth = max(max_depth, depth) | ||
stack.append((node.left, depth + 1)) | ||
stack.append((node.right, depth + 1)) | ||
|
||
return max_depth |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution: | ||
def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
if root is None: | ||
return 0 | ||
max_depth = 0 | ||
stack = [(root, 1)] | ||
while stack: | ||
node, depth = stack.pop() | ||
max_depth = max(max_depth, depth) | ||
if node.left is not None: | ||
stack.append((node.left, depth + 1)) | ||
if node.right is not None: | ||
stack.append((node.right, depth + 1)) | ||
|
||
return max_depth |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# 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 maxDepth(self, root: Optional[TreeNode]) -> int: | ||
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. nodes_and_depthsなどとしてもいい気がします |
||
max_depth = 0 | ||
|
||
while stack: | ||
node, depth = stack.pop() | ||
if node is None: | ||
continue | ||
max_depth = max(max_depth, depth) | ||
stack.append((node.left, depth + 1)) | ||
stack.append((node.right, depth + 1)) | ||
|
||
return max_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. 特に問題ないと思います。 |
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.
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 が何なのかすらわかっていない)。
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