Compare commits

..

No commits in common. "1eaa8169f36d5d60ccf866b02f78895bb7da59bc" and "29c55c38a9d0ef633163ed5c546a020a8881f8d1" have entirely different histories.

1 changed files with 4 additions and 6 deletions

View File

@ -11,20 +11,18 @@ package com.cool.hot100.binary_tree;
*/ */
public class Num543 { public class Num543 {
int max=0;
public int diameterOfBinaryTree(TreeNode root) { public int diameterOfBinaryTree(TreeNode root) {
if (root == null) return 0; if (root == null) return 0;
int left = getTreeDeep(root.left); int left = getTreeDeep(root.left);
int right = getTreeDeep(root.right); int right = getTreeDeep(root.right);
return Math.max(max,left+right); return left + right;
} }
private int getTreeDeep(TreeNode root) { private int getTreeDeep(TreeNode root) {
if (root == null) return 0; if (root == null) return 0;
int left = getTreeDeep(root.left); int res = getTreeDeep(root.left);
int right=getTreeDeep(root.right); res = Math.max(res, getTreeDeep(root.right));
max = Math.max(max,left+right ); return res + 1;
return Math.max(left,right)+1;
} }
} }