Skip to content

Commit 9d9ad5b

Browse files
authored
Update 236-lowest-common-ancestor-of-a-binary-tree.js
1 parent 2ed3303 commit 9d9ad5b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

236-lowest-common-ancestor-of-a-binary-tree.js

+23
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,26 @@ const lowestCommonAncestor = function(root, p, q) {
5555
if(left && right) return root;
5656
return left ? left : right;
5757
};
58+
59+
// another
60+
61+
/**
62+
* Definition for a binary tree node.
63+
* function TreeNode(val) {
64+
* this.val = val;
65+
* this.left = this.right = null;
66+
* }
67+
*/
68+
/**
69+
* @param {TreeNode} root
70+
* @param {TreeNode} p
71+
* @param {TreeNode} q
72+
* @return {TreeNode}
73+
*/
74+
const lowestCommonAncestor = function(root, p, q) {
75+
if(root == null || root === p || root === q) return root
76+
const left = lowestCommonAncestor(root.left, p, q)
77+
const right = lowestCommonAncestor(root.right, p, q)
78+
if(left && right) return root
79+
return left || right
80+
};

0 commit comments

Comments
 (0)