728x90
class Solution:
# @param {TreeNode} root
# @param {TreeNode} p
# @param {TreeNode} q
# @return {TreeNode}
def lowestCommonAncestor(self, root, p, q):
s, b = sorted([p.val, q.val])
while not s <= root.val <= b:
# Keep searching since root is outside of [s, b].
root = root.left if s <= root.val else root.right
# s <= root.val <= b.
return root
출처: github.com/jiapengwen/LeetCode/blob/master/Python/lowest-common-ancestor-of-a-binary-search-tree.py
728x90
'Leetcode' 카테고리의 다른 글
[LeetCode] Valid Anagram 파이썬 (dict) (0) | 2021.04.07 |
---|---|
[LeetCode] Delete Node in a Linked List 파이썬 (linked list) (0) | 2021.04.07 |
[LeetCode] Palindrome Linked List 파이썬 (0) | 2021.04.06 |
[LeetCode] Summary Ranges 파이썬 (0) | 2021.04.06 |
[LeetCode] Invert Binary Tree 파이썬(recursion) (0) | 2021.04.05 |
댓글