-
Notifications
You must be signed in to change notification settings - Fork 0
104. Maximum Depth of Binary Tree #23
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
Open
xbam326
wants to merge
3
commits into
main
Choose a base branch
from
104
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,52 @@ | ||
| ## step1 | ||
| - 幅優先探索か深さ優先探索で解けそう | ||
| - 幅優先探索の方が描き慣れてるので幅優先で行く | ||
| - Nodeが0の場合もある | ||
| - 最大でも10^4なので計算量O(n)で問題なし | ||
| - 5分ほどで書けた | ||
| - rootがNoneの場合でWAを出してしまった | ||
| - 気をつけてはいたが `while [null]:`がTrueになってwhileの中が実行された | ||
| - `if root is None: return 0`と最初に処理するようにしたがもっと良い感じに書く方法があれば書きたい | ||
|
|
||
| ## step2 | ||
| - 他の人のコードを見る | ||
| - https://github.com/mamo3gr/arai60/pull/21 | ||
| - 再帰に慣れていないので理解に時間がかかった | ||
| - 意外とコメントにあった`return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1`の方が1行で複雑そうなのに理解しやすく感じた | ||
| - `to_visit`は幅広く使えて良い命名だと思った | ||
| - コメントにあった下記はかっこいいけど難しく感じる | ||
| ```py | ||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| return max(self.maxDepth(child) for child in (root.left, root.right)) + 1 | ||
| ``` | ||
| - https://github.com/Yuto729/LeetCode_arai60/pull/26/files | ||
| - tupleにしてdepthを一緒に更新していくのもありだと思ったし選択肢になかった | ||
| - `こちらのコードでは先にnullチェックを行っていますが,とりあえずスタックに追加してからnullチェックをする手もあります:`これも選択肢になかった | ||
| - ほぼ同じコードを書いたが`[root] の中身があることによって、root の depth が1だと分かるわけですね。`というコメントもあるので参考にする。 | ||
|
|
||
| - https://github.com/plushn/SWE-Arai60/pull/21/files | ||
| - `pop直後にやる方法について、prosはNoneチェックを現状のコードで3箇所あるのを1箇所に集約できる点、consは一時的にNoneがキューに入ることでループが多く回る点です。BFSにて幾何級数的に増加することがあるそうです。` | ||
| - `if node.left: はNoneを弾く意図だと思いますが、注意深い読み手はspecial method TreeNode.__bool__()がオーバーライドされてないか気になってしまいます。また実際にTreeNode.__bool__()の実装次第ではnode.leftがNoneでなくとも意図せずimplicit falsyが成立しうる点が危ういと感じるので、個人的にはif node.left is not Noneの方が好ましいと感じます。` | ||
|
|
||
| - step2は苦手意識がある再帰でやってみる | ||
| - 書いた結果、書く分量が劇的に減るので書く際の時間や読んで理解するまでの時間も考えこちらの方が良い気がしてきた | ||
| - ただどのような順番でstackに積まれていくかをイメージするのが苦手なのでstackの数が無闇に増えていないことを確認するのに時間がかかった | ||
| - 今回は木の深さがmax(10程度なはず)なのでpythonのデフォルトの1000以下で問題なし | ||
| A=>B=>D=>E=>C | ||
| ``` | ||
| A | ||
| / \ | ||
| B C | ||
| / \ | ||
| D E | ||
|
|
||
| ``` | ||
|
|
||
| ## step3 | ||
| - step3はdepthも一緒に更新していく方法にした | ||
| - どの方法も5分以内に書けた | ||
|
|
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,34 @@ | ||
| # | ||
| # @lc app=leetcode id=104 lang=python3 | ||
| # | ||
| # [104] Maximum Depth of Binary Tree | ||
| # | ||
|
|
||
| # @lc code=start | ||
| # 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: | ||
| depth = 0 | ||
| if root is None: | ||
| return 0 | ||
| parents = [root] | ||
| while parents: | ||
| children = [] | ||
| for parent in parents: | ||
| if parent.left: | ||
| children.append(parent.left) | ||
| if parent.right: | ||
| children.append(parent.right) | ||
|
|
||
| parents = children | ||
| depth += 1 | ||
|
|
||
| return depth | ||
|
|
||
|
|
||
| # @lc code=end |
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 @@ | ||
| # | ||
| # @lc app=leetcode id=104 lang=python3 | ||
| # | ||
| # [104] Maximum Depth of Binary Tree | ||
| # | ||
|
|
||
| # @lc code=start | ||
| # 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: | ||
| if root is None: | ||
| return 0 | ||
| return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 | ||
|
|
||
|
|
||
| # @lc code=end |
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,33 @@ | ||
| # | ||
| # @lc app=leetcode id=104 lang=python3 | ||
| # | ||
| # [104] Maximum Depth of Binary Tree | ||
| # | ||
|
|
||
| # @lc code=start | ||
| # 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: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| to_visit = [(root, 1)] | ||
| max_depth = 0 | ||
|
|
||
| while to_visit: | ||
| node, depth = to_visit.pop() | ||
| max_depth = max(depth, max_depth) | ||
| if node.left is not None: | ||
| to_visit.append((node.left, depth + 1)) | ||
| if node.right is not None: | ||
| to_visit.append((node.right, depth + 1)) | ||
|
|
||
| return max_depth | ||
|
|
||
|
|
||
| # @lc code=end | ||
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.
[fyi]
私はあんまり違和感ないのですが、「to + 動詞」が通じにくいケースもありそうでした。
https://github.com/mamo3gr/arai60/pull/23/changes#r2674256708