Skip to content

Commit 721187f

Browse files
committed
提交110
1 parent bac508e commit 721187f

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

leetcode/src/main/java/com/mistray/tree/BalancedBinaryTree110.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,30 @@
88
* @Desc
99
*/
1010
public class BalancedBinaryTree110 {
11-
}
11+
12+
public static void main(String[] args) {
13+
14+
}
15+
16+
int max = 0;
17+
18+
public int diameterOfBinaryTree(TreeNode root) {
19+
if (root == null) {
20+
return 0;
21+
}
22+
max(root);
23+
return max;
24+
}
25+
26+
public int max(TreeNode root) {
27+
if (root == null) {
28+
return 0;
29+
}
30+
int L = max(root.left);
31+
int R = max(root.right);
32+
max = Math.max(max, L + R);
33+
// 返回的是当前节点子树的最大深度,左节点和右节点对比后得到较大值后再加一.
34+
// 加一的原因是,需要算当前树高.
35+
return Math.max(L, R) + 1;
36+
}
37+
}

0 commit comments

Comments
 (0)