Python O(log n) time O(1) space BST LowesCommonAncestor


  • 0
    B
    class Solution:
        # @param {TreeNode} root
        # @param {TreeNode} p
        # @param {TreeNode} q
        # @return {TreeNode}
        def lowestCommonAncestor(self, root, p, q):
            node = root
            if p == None or q == None: 
                return None
            while node:
                print node.val
                if p.val < node.val and q.val < node.val:
                    node = node.left
                elif p.val > node.val and q.val > node.val:
                    node = node.right
                else:
                    break
            return node

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.