116 ms Python Solution 97.45% non-recursive, easy to understand


  • 1
    W
    class Solution(object):
        def lowestCommonAncestor(self, root, p, q):
            """
            :type root: TreeNode
            :type p: TreeNode
            :type q: TreeNode
            :rtype: TreeNode
            """
            result = root
            while result:
                if result.val > p.val and result.val > q.val and result.left:
                    result = result.left 
                    continue
                elif result.val < p.val and result.val < q.val and result.right:
                    result = result.right
                    continue
                break
            return result

Log in to reply
 

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