-
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
ryosuketc
wants to merge
1
commit into
main
Choose a base branch
from
104_maximum_depth_of_binary_tree
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
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,22 @@ | ||
| # 104. Maximum Depth of Binary Tree | ||
|
|
||
| https://leetcode.com/problems/maximum-depth-of-binary-tree/ | ||
|
|
||
| ## Comments | ||
|
|
||
| ### step1 | ||
|
|
||
| * まあ再帰で書こうかな、と思って 4:00 で AC | ||
| * `root` みたいな引数の関数を使うとなんか認知不可が高い気がする (`root` は木全体の根であって、再帰それぞれの根であるという感覚がないからな気がする) | ||
|
|
||
| ### step2 | ||
|
|
||
| * 引数名を `node` (`root` ではなく) にしたかったので、`GetDepth` のような private 関数を使おうかと思ったが、結局 depth って定義上はより深い方を返す (そうでないなら、右と左の pair を返すとかはできるけど) ということになり、そうすると `maxDepth` と全く同じ機能になるので分ける必要もない気がした。 | ||
| * 一方でまあ深さを追加の引数として取って再帰するなら別の関数にする意味はある | ||
| * Arai60 で解いている | ||
| * https://github.com/ryosuketc/leetcode_arai60/pull/21/files | ||
| * 再帰のスタックオーバーフローとかを気にするなら、上記の通り iterative な解き方もあるかもしれない。上記は Python で書いたが、C++ なら `step2.Solution` みたいな感じかな。 | ||
|
|
||
| ### step3 | ||
|
|
||
| * 省略 |
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,20 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * struct TreeNode { | ||
| * int val; | ||
| * TreeNode *left; | ||
| * TreeNode *right; | ||
| * TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
| * }; | ||
| */ | ||
| class Solution { | ||
| public: | ||
| int maxDepth(TreeNode* root) { | ||
| if (!root) { | ||
| return 0; | ||
| } | ||
| return std::max(maxDepth(root->left), maxDepth(root->right)) + 1; | ||
| } | ||
| }; |
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 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * struct TreeNode { | ||
| * int val; | ||
| * TreeNode *left; | ||
| * TreeNode *right; | ||
| * TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
| * }; | ||
| */ | ||
|
|
||
| #include <stack> | ||
|
|
||
| class Solution { | ||
| public: | ||
| int maxDepth(TreeNode* root) { | ||
| std::stack<std::pair<TreeNode*, int>> node_and_depth; | ||
| node_and_depth.push(std::make_pair(root, 1)); | ||
| int max_depth = 0; | ||
| while (!node_and_depth.empty()) { | ||
| auto [node, depth] = node_and_depth.top(); | ||
| node_and_depth.pop(); | ||
| if (!node) { | ||
| continue; | ||
| } | ||
| max_depth = std::max(max_depth, depth); | ||
| node_and_depth.push(std::make_pair(node->left, depth + 1)); | ||
| node_and_depth.push(std::make_pair(node->right, depth + 1)); | ||
| } | ||
| return max_depth; | ||
| } | ||
| }; | ||
Empty file.
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.
自分ならここで if (node->left) し、無駄な push() と pop() を省くのですが、趣味の範囲だと思います。