Simple python in-order non-recursive traversal


  • 2
    Y
    class Solution(object):
        def kthSmallest(self, root, k):
            """
            :type root: TreeNode
            :type k: int
            :rtype: int
            """
            
            parents = []
            node = root
            while parents or node:
                if node:
                    parents.append(node)
                    node = node.left
                else:
                    node = parents.pop()
                    if k > 1:
                       k -= 1
                    else:
                        return node.val
                        
                    node = node.right

Log in to reply
 

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