Skip to content

Commit 1281511

Browse files
authored
Create 0236-lowest-common-ancestor-of-a-binary-tree.java
1 parent 715a312 commit 1281511

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
3+
if (root == null) return null;
4+
if (p == root || q == root) return root;
5+
6+
TreeNode left = lowestCommonAncestor(root.left, p, q);
7+
TreeNode right = lowestCommonAncestor(root.right, p, q);
8+
9+
if (left != null && right != null) return root;
10+
else if (left != null) return left;
11+
else return right;
12+
}
13+
}

0 commit comments

Comments
 (0)