Skip to content

Commit 943b2a1

Browse files
iamAntimPalAntim-IWPIamShiwangi
committed
Update 236. Lowest Common Ancestor of a Binary Tree.py
Co-Authored-By: Antim-IWP <[email protected]> Co-Authored-By: Shiwangi Srivastava <[email protected]>
1 parent 9ddfb3e commit 943b2a1

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
9+
class Solution:
10+
def lowestCommonAncestor(
11+
self, root: "TreeNode", p: "TreeNode", q: "TreeNode"
12+
) -> "TreeNode":
13+
if root in (None, p, q):
14+
return root
15+
left = self.lowestCommonAncestor(root.left, p, q)
16+
right = self.lowestCommonAncestor(root.right, p, q)
17+
return root if left and right else (left or right)

0 commit comments

Comments
 (0)