Easy to follow iterative Python solution


  • 0
    N
    class Solution(object):
        def lowestCommonAncestor(self, root, p, q):
            """
            :type root: TreeNode
            :type p: TreeNode
            :type q: TreeNode
            :rtype: TreeNode
            """
            while root:
                if p.val < root.val and q.val < root.val:
                    root = root.left
                elif p.val > root.val and q.val > root.val:
                    root = root.right
                else:
                    return root.val
    

Log in to reply
 

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