Python Inorder Morris Traversal


  • 0
    S
            cur, pre, tmp = root, None, None
            while cur:
                if not cur.left:
                    if pre and pre.val >= cur.val:
                        return False
                    pre = cur
                    cur = cur.right
                else:
                    tmp = cur.left
                    while tmp.right and tmp.right != cur:
                        tmp = tmp.right
                    if not tmp.right:
                        tmp.right = cur
                        cur = cur.left
                    else:
                        tmp.right = None
                        if pre and pre.val >= cur.val:
                            return False
                        pre = cur
                        cur = cur.right
            return True 
    

Log in to reply
 

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