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 18db2b6

Browse files
236. Lowest Common Ancestor of a Binary Tree
1 parent a0a9ac8 commit 18db2b6

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode(var `val`: Int = 0) {
4+
* var left: TreeNode? = null
5+
* var right: TreeNode? = null
6+
* }
7+
*/
8+
9+
class Solution {
10+
fun lowestCommonAncestor(root: TreeNode?, p: TreeNode?, q: TreeNode?): TreeNode? {
11+
if(root == null || root == p || root == q ) return root
12+
var left = lowestCommonAncestor(root.left, p, q)
13+
var right = lowestCommonAncestor(root.right, p, q)
14+
return if (left != null && right != null) root else if (left != null) left else right
15+
}
16+
}

0 commit comments

Comments
(0)

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