Skip to content

Commit 28768ae

Browse files
committed
1123. Lowest Common Ancestor of Deepest Leaves
1 parent 5d99364 commit 28768ae

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

leetcode/medium/LCADeepestLeaves.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package leetcode.medium;
2+
3+
import leetcode.easy.TreeNode;
4+
5+
public class LCADeepestLeaves {
6+
public TreeNode lcaDeepestLeaves(TreeNode root) {
7+
int heightLeft = height(root.left);
8+
int heightRight = height(root.right);
9+
10+
if (heightLeft < heightRight) {
11+
return lcaDeepestLeaves(root.right);
12+
}
13+
14+
if (heightLeft > heightRight) {
15+
return lcaDeepestLeaves(root.left);
16+
}
17+
18+
return root;
19+
}
20+
21+
public int height(TreeNode root) {
22+
if (root == null) {
23+
return 0;
24+
}
25+
26+
int heightLeft = height(root.left);
27+
int heightRight = height(root.right);
28+
29+
return 1 + Math.max(heightLeft, heightRight);
30+
}
31+
}

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ EASY
160160
|[1100. Find K-Length Substrings With No Repeated Characters](https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters)|[java](https://github.com/wdydev/leetcode/blob/master/leetcode/medium/KLengthSubstringNoRepeatedCharacters.java)|
161161
|[1108. Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address)|[java](https://github.com/wdydev/leetcode/blob/master/leetcode/easy/DefangIPAddress.java)|
162162
|[1119. Remove Vowels from a String](https://leetcode.com/problems/remove-vowels-from-a-string)|[java](https://github.com/wdydev/leetcode/blob/master/leetcode/easy/RemoveVowelsFromString.java)|
163-
|[]()|[java]()|
163+
|[1123. Lowest Common Ancestor of Deepest Leaves](https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves)|[java](https://github.com/wdydev/leetcode/blob/master/leetcode/easy/LCADeepestLeaves.java)|
164164
|[]()|[java]()|
165165
|[]()|[java]()|
166166
|[]()|[java]()|

0 commit comments

Comments
 (0)