Skip to content

Commit 6d97934

Browse files
authored
Create 235-lowest-common-ancestor-of-a-binary-search-tree.js
1 parent 9bf836f commit 6d97934

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
const lowestCommonAncestor = function(root, p, q) {
15+
while((root.val - p.val) * (root.val - q.val) > 0) {
16+
root = root.val > p.val ? root.left : root.right
17+
}
18+
return root
19+
};

0 commit comments

Comments
 (0)