Skip to content

10_Lowest Common Ancestor of a Binary Search Tree.md#13

Open
Kitaken0107 wants to merge 1 commit intomainfrom
Kitaken0107-patch-14
Open

10_Lowest Common Ancestor of a Binary Search Tree.md#13
Kitaken0107 wants to merge 1 commit intomainfrom
Kitaken0107-patch-14

Conversation

@Kitaken0107
Copy link
Owner

Copy link

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

再帰でも解いてみると良いと思います。

def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':

while root:
if root.val > p.val and root.val > q.val:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ、< で統一したほうが読みやすくないですか?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私の趣味はこんなんですね。

if p.val < root.val and q.val < root.val:
root = root.left
continue
if root.val < p.val and root.val < q.val:
root = root.right
continue
return root

両方左にあったら左に行って、再挑戦。
両方右にあったら右に行って、再挑戦。
間に入ったらそれ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants