Advertisement
vkreal

Untitled

May 16th, 2022
424
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Definition for a binary tree node.
  3.  * function TreeNode(val) {
  4.  *     this.val = val;
  5.  *     this.left = this.right = null;
  6.  * }
  7.  */
  8. /**
  9.  * @param {TreeNode} root
  10.  * @param {TreeNode} p
  11.  * @param {TreeNode} q
  12.  * @return {TreeNode}
  13.  */
  14. var lowestCommonAncestor = function(root, p, q) {
  15.     if(!root || root == p || root == q) return root;
  16.     let left = lowestCommonAncestor(root.left, p, q);
  17.     let right = lowestCommonAncestor(root.right, p, q);
  18.     return left && right ? root : left || right;
  19. };
Advertisement
RAW Paste Data Copied
Advertisement