-
Notifications
You must be signed in to change notification settings - Fork 0
104. Maximum Depth of Binary Tree #10
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: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| ```java | ||
| // Use DFS, return depth when node don't have children | ||
| // Increse depth before visit child node | ||
| // Time complexity: O(N) N: number of node | ||
| // Space complexity: O(N) | ||
| // Time spend: 04:26 | ||
| class Solution { | ||
| public int maxDepth(TreeNode root) { | ||
| if (root == null) { | ||
| return 0; | ||
| } | ||
| return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; | ||
| } | ||
|
|
||
| // BFS | ||
| // Time spend: 04:30 | ||
| public int maxDepthFindByBfs(TreeNode root) { | ||
| if (root == null) { | ||
| return 0; | ||
| } | ||
|
|
||
| Queue<TreeNode> queue = new LinkedList<>(); | ||
| queue.add(root); | ||
|
|
||
| int maxDepth = 0; | ||
| while (!queue.isEmpty()) { | ||
| int queueSize = queue.size(); | ||
| for (int i = 0; i < queueSize; i++) { | ||
| TreeNode curr = queue.poll(); | ||
| if (curr.left != null) { | ||
| queue.add(curr.left); | ||
| } | ||
| if (curr.right != null) { | ||
| queue.add(curr.right); | ||
| } | ||
| } | ||
| maxDepth++; | ||
| } | ||
|
|
||
| return maxDepth; | ||
| } | ||
| } | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| ```java | ||
| class Solution { | ||
| public int maxDepth(TreeNode root) { | ||
| return maxDepthFindByBfs(root); | ||
| } | ||
|
|
||
| public int maxDepthFindByDfs(TreeNode root) { | ||
| if (root == null) { | ||
| return 0; | ||
| } | ||
|
|
||
| return Math.max(maxDepthFindByDfs(root.left), maxDepthFindByDfs(root.right)) + 1; | ||
| } | ||
|
|
||
| // BFS | ||
| // Time spend: 04:30 | ||
| public int maxDepth(TreeNode root) { | ||
| if (root == null) { | ||
| return 0; | ||
| } | ||
|
|
||
| Queue<Pair<TreeNode, Integer>> queue = new LinkedList<>(); | ||
| queue.add(new Pair(root, 1)); | ||
|
|
||
| int maxDepth = 1; | ||
| while (!queue.isEmpty()) { | ||
| Pair<TreeNode, Integer> current = queue.poll(); | ||
| maxDepth = Math.max(current.getValue(), maxDepth); | ||
|
|
||
| if (current.getKey().left != null) { | ||
| queue.add(new Pair(current.getKey().left, current.getValue() + 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. Pair の使用は極力避けましょう。理由は、 key と value に何が含まれているかをソースコードから読み取るのにやや認知負荷がかかるためです。 等、ユーザー型を定義し、それに入れたほうが読みやすいです。
Owner
Author
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. プロダクトに使うことでしたらそうしますが、面接の時は定義の実装少し面倒なため、一言普段こうやって書きませんよってやんわり流しながらPair使うのは許されるものでしょうか? 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. はい、大丈夫だと思います。 |
||
| } | ||
| if (current.getKey().right != null) { | ||
| queue.add(new Pair(current.getKey().right, current.getValue() + 1)); | ||
| } | ||
| } | ||
|
|
||
| return maxDepth; | ||
| } | ||
| } | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| ```java | ||
| // Time spend: 02:11 | ||
| class Solution { | ||
| public int maxDepth(TreeNode root) { | ||
| return maxDepthByDfs(root, 1); | ||
| } | ||
|
|
||
| public int maxDepthByDfs(TreeNode root, int currentDepth) { | ||
| if (root == null) { | ||
| return currentDepth - 1; | ||
| } | ||
|
|
||
| int nextDepth = currentDepth + 1; | ||
| return Math.max(maxDepthByDfs(root.left, nextDepth), | ||
| maxDepthByDfs(root.right, nextDepth)); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```java | ||
| // Time spend: 02:03 | ||
| class Solution { | ||
| public int maxDepth(TreeNode root) { | ||
| return maxDepthByDfs(root, 1); | ||
| } | ||
|
|
||
| public int maxDepthByDfs(TreeNode root, int currentDepth) { | ||
| if (root == null) { | ||
| return currentDepth - 1; | ||
| } | ||
|
|
||
| int nextDepth = currentDepth + 1; | ||
| return Math.max(maxDepthByDfs(root.left, nextDepth), | ||
| maxDepthByDfs(root.right, nextDepth)); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```java | ||
| // Time spend: 01:42 | ||
| class Solution { | ||
| public int maxDepth(TreeNode root) { | ||
| return maxDepthByDfs(root, 1); | ||
| } | ||
|
|
||
| public int maxDepthByDfs(TreeNode root, int currentDepth) { | ||
| if (root == null) { | ||
| return currentDepth - 1; | ||
| } | ||
|
|
||
| int nextDepth = currentDepth + 1; | ||
| return Math.max(maxDepthByDfs(root.left, nextDepth), maxDepthByDfs(root.right, nextDepth)); | ||
| } | ||
| } | ||
| ``` |
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.
queueのサイズをあらかじめ取得し、そのサイズ分だけ処理をするという処理は、普段見ないように思います。 queue の中に TreeNode と 深さの両方を入れるか、現在処理中のキューと、次に処理予定のキューに分けるほうが一般的だと思います。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.
木構造の階層ごとに処理を挟みたいからこの処理にしています。
Pairで深さを入れる方が良さそうですね。