Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e229149

Browse files
[ACCEPTED][Problem 1123]Lowest Common Ancestor of Deepest Leaves
1 parent c36d853 commit e229149

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

‎src/medium/LowestCommonAncestorOfDeepestLeaves1123.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ package medium
22

33
object LowestCommonAncestorOfDeepestLeaves1123 {
44
fun lcaDeepestLeaves(root: TreeNode?): TreeNode? {
5+
return dfs(root).first
6+
}
7+
8+
private fun dfs(root: TreeNode?): Pair<TreeNode?,Int>{
9+
if(root == null)
10+
{
11+
return Pair(null,0)
12+
}
13+
14+
val left = dfs(root.left)
15+
val right = dfs(root.right)
516

17+
if(left.second> right.second)
18+
{
19+
return Pair(left.first,left.second+1)
20+
}
21+
if(left.second < right.second){
22+
return Pair(right.first,right.second+1)
23+
}
24+
return Pair(root,left.second+1)
625
}
726
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /