Easy InOrder Binary Tree Traversal using Python


  • 0
    W
    class Solution(object):
        def kthSmallest(self, root, k):
            """
            :type root: TreeNode
            :type k: int
            :rtype: int
            """
            if root==None:
                return []
            s,re,node=[],[],root
            while s or node:
                if node:
                    s.append(node)
                    if node.left:
                        node=node.left
                    else:
                        node=None
                else:
                    node=s.pop(-1)
                    re.append(node.val)
                    if node.right:
                        node=node.right
                    else:
                        node=None
            return re[k-1]

Log in to reply
 

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