Skip to content

Commit 5818704

Browse files
authored
Update 235-lowest-common-ancestor-of-a-binary-search-tree.js
1 parent 17b9b91 commit 5818704

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

235-lowest-common-ancestor-of-a-binary-search-tree.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
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+
/**
10+
* @param {TreeNode} root
11+
* @param {TreeNode} p
12+
* @param {TreeNode} q
13+
* @return {TreeNode}
14+
*/
15+
const lowestCommonAncestor = function(root, p, q) {
16+
if(root == null || root == p || root == q) return root
17+
const left = lowestCommonAncestor(root.left, p, q)
18+
const right = lowestCommonAncestor(root.right, p, q)
19+
if(left && right) return root
20+
return left || right
21+
};
22+
23+
// another
24+
125
/**
226
* Definition for a binary tree node.
327
* function TreeNode(val) {

0 commit comments

Comments
 (0)