1123. Lowest Common Ancestor of Deepest Leaves
class Solution {
public TreeNode lcaDeepestLeaves(TreeNode root) {
if (root == null) {
return null;
}
int left = getDepth(root.left);
int right = getDepth(root.right);
if (left == right) {
return root;
}
return (left > right) ? lcaDeepestLeaves(root.left) : lcaDeepestLeaves(root.right);
}
private int getDepth(TreeNode root) {
if(root == null) {
return 0;
}
return Math.max(getDepth(root.left), getDepth(root.right))+1;
}
}Previous1676. Lowest Common Ancestor of a Binary Tree IVNext2096. Step-By-Step Directions From a Binary Tree Node to Another
Last updated