最深叶结点的最近公共祖先
class Solution {
public TreeNode lcaDeepestLeaves(TreeNode root) {
int left=depth(root.left);
int right=depth(root.right);
if(left<right){
return lcaDeepestLeaves(root.right);
}else if(left>right){
return lcaDeepestLeaves(root.left);
}else{
return root;
}
}
public int depth(TreeNode root){
if(root==null){
return 0;
}
int left=depth(root.left);
int right=depth(root.right);
return Math.max(left,right)+1;
}
}