Skip to content

Commit d3dd3be

Browse files
authored
Create 1123-lowest-common-ancestor-of-deepest-leaves.js
1 parent 71ae9ca commit d3dd3be

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
* @return {TreeNode}
11+
*/
12+
const lcaDeepestLeaves = function(root) {
13+
if (root === null) return null
14+
const getHeight = root => {
15+
if (root === null) return 0
16+
const res = Math.max(getHeight(root.left), getHeight(root.right)) + 1
17+
return res
18+
}
19+
if (getHeight(root.left) === getHeight(root.right)) {
20+
return root
21+
} else if (getHeight(root.left) > getHeight(root.right)) {
22+
return lcaDeepestLeaves(root.left)
23+
} else {
24+
return lcaDeepestLeaves(root.right)
25+
}
26+
}

0 commit comments

Comments
 (0)