We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bac508e commit 721187fCopy full SHA for 721187f
leetcode/src/main/java/com/mistray/tree/BalancedBinaryTree110.java
@@ -8,4 +8,30 @@
8
* @Desc
9
*/
10
public class BalancedBinaryTree110 {
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
28
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