MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / Solution

Class Solution

Tree/LowestCommonAncestorOfABinarySearchTree.py:57–79  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

55# self.right = None
56
57class Solution(object):
58 def lowestCommonAncestor(self, root, p, q):
59 """
60 :type root: TreeNode
61 :type p: TreeNode
62 :type q: TreeNode
63 :rtype: TreeNode
64 """
65
66 if p.val == root.val or q.val == root.val:
67 return root
68
69 if p.val < root.val and q.val > root.val:
70 return root
71 elif p.val > root.val and q.val < root.val:
72 return root
73
74 if p.val > root.val and q.val > root.val:
75
76 return self.lowestCommonAncestor(root.right, p, q)
77
78 else:
79 return self.lowestCommonAncestor(root.left, p, q)
80

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected