Simple Java Solution


  • 13
    C
    If the given both nodes values are less than that of root, then both the nodes must be on the left side of the root , so now we have to check only left tree of the root.
    Otherwise If the given both nodes values are greater than that of root, then both the nodes must be on the right side of the root , so now we have to check only right tree of the root.
    Otherwise . both the nodes will be on the either side of the root, this implies the lowest common ancestor is root.
    
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            if(p.val < root.val && q.val < root.val){
            return lowestCommonAncestor(root.left,p,q);}
            else if(p.val > root.val && q.val > root.val){
            return lowestCommonAncestor(root.right,p,q);}
            else{
            return root;}
        }

  • 0
    C

    Nice and clear solution!


  • 0
    D

    what if one of the target is not actually in the tree?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.