JavaScript solution


  • 4
    L
    function lowestCommonAncestor(root, p, q) {
      if (!root || root === p || root === q) return root;
      var resL = lowestCommonAncestor(root.left, p, q);
      var resR = lowestCommonAncestor(root.right, p, q);
      return (resL && resR) ? root : (resL || resR);
    }

  • 0
    C

    Cool! Very good understanding of recursion.


Log in to reply
 

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