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 016288b

Browse files
2020年10月06日
1 parent 468f8bd commit 016288b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def lowestCommonAncestor(self, root, p, q):
10+
"""
11+
:type root: TreeNode
12+
:type p: TreeNode
13+
:type q: TreeNode
14+
:rtype: TreeNode
15+
"""
16+
if not root:
17+
return None
18+
if root == p or root == q:
19+
return root
20+
left = self.lowestCommonAncestor(root.left, p, q)
21+
right = self.lowestCommonAncestor(root.right, p, q)
22+
23+
if left and right:
24+
return root
25+
if left:
26+
return left
27+
if right:
28+
return right
29+
return None

0 commit comments

Comments
(0)

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