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

Class Solution

Tree/LowestCommonAncestorOfABinaryTree.py:65–91  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

63# self.right = None
64
65class Solution(object):
66 def lowestCommonAncestor(self, root, p, q):
67 """
68 :type root: TreeNode
69 :type p: TreeNode
70 :type q: TreeNode
71 :rtype: TreeNode
72 """
73 if root.val == p.val or root.val == q.val:
74 return root
75
76 right = None
77 left = None
78
79 if root.right:
80 right = self.lowestCommonAncestor(root.right, p, q)
81 if root.left:
82 left = self.lowestCommonAncestor(root.left, p, q)
83
84 if right and left:
85 return root
86
87 if right:
88 return right
89
90 if left:
91 return left

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected