标签:val root 最大的 左右子树 nod node solution return code
实际上是找出某左右子树深度和最大的结点。不一定是根结点!!!
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int ans;
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
ans = Math.max(ans, left+right);
return Math.max(left, right)+1;
}
public int diameterOfBinaryTree(TreeNode root) {
if(root == null) return 0;
ans = 0;
maxDepth(root);
return ans;
}
}
leetcode 543 Diameter of Binary Tree
标签:val root 最大的 左右子树 nod node solution return code
原文地址:https://www.cnblogs.com/ctqchina/p/10111391.html