-
Notifications
You must be signed in to change notification settings - Fork 43
/
MaximumDepthOfBinaryTree.java
126 lines (103 loc) · 3.31 KB
/
MaximumDepthOfBinaryTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package LeetCodeJava.Recursion;
// https://leetcode.com/problems/maximum-depth-of-binary-tree/
import LeetCodeJava.DataStructure.TreeNode;
import java.util.LinkedList;
public class MaximumDepthOfBinaryTree {
// V0
// IDEA : RECURSIVE
public int maxDepth(TreeNode root) {
if (root == null){
return 0;
}
// NOTE : below conditon is optional (have or not use is OK)
// if (root.left == null && root.right == null){
// return 1;
// }
/** NOTE !!! we get max left, right depth first, then return bigger one as final answer */
int leftD = maxDepth(root.left) + 1;
int rightD = maxDepth(root.right) + 1;
return Math.max(leftD, rightD);
}
// V0'
// NOTE !!! below is wrong, we should get max left, right depth first, then compare them
// public int maxDepth(TreeNode root) {
//
// if (root == null){
// return 0;
// }
//
// if (root.left != null){
// return 1 + this.maxDepth(root.left);
// }
//
// if (root.right != null){
// return 1 + this.maxDepth(root.right);
// }
//
// return Math.max(this.maxDepth(root.left), this.maxDepth(root.right));
// }
// V0''
// IDEA : RECURSIVE
public int maxDepth_1(TreeNode root) {
if (root == null){
return 0;
}
return Math.max(maxDepth_1(root.left) + 1, maxDepth_1(root.right) + 1);
}
// V0''
// IDEA : DFS (RECURSION)
public int maxDepth__(TreeNode root) {
if (root == null){
return 0;
}
int cur = 0;
int depth = 0;
return dfs(root, cur, depth);
}
private int dfs(TreeNode root, int cur, int depth){
if (root == null){
depth = Math.max(cur, depth);
return depth;
}
return Math.max(dfs(root.left, cur+1, depth), dfs(root.right, cur+1, depth));
}
// V1
// IDEA : RECURSION
// https://leetcode.com/problems/maximum-depth-of-binary-tree/editorial/
public int maxDepth_2(TreeNode root) {
if (root == null) {
return 0;
} else {
int left_height = maxDepth_2(root.left);
int right_height = maxDepth_2(root.right);
return java.lang.Math.max(left_height, right_height) + 1;
}
}
// V2
// IDEA : Tail Recursion + BFS
// https://leetcode.com/problems/maximum-depth-of-binary-tree/editorial/
// only available in C++ (not in Java, Python)
// V3
// IDEA : Iteration
// https://leetcode.com/problems/maximum-depth-of-binary-tree/editorial/
public int maxDepth_3(TreeNode root) {
LinkedList<TreeNode> stack = new LinkedList<>();
LinkedList<Integer> depths = new LinkedList<>();
if (root == null) return 0;
stack.add(root);
depths.add(1);
int depth = 0, current_depth = 0;
while(!stack.isEmpty()) {
root = stack.pollLast();
current_depth = depths.pollLast();
if (root != null) {
depth = Math.max(depth, current_depth);
stack.add(root.left);
stack.add(root.right);
depths.add(current_depth + 1);
depths.add(current_depth + 1);
}
}
return depth;
}
}