Python solution!


  • 0
    H
    # Definition for a binary tree node.
        # class TreeNode:
        #     def __init__(self, x):
        #         self.val = x
        #         self.left = None
        #         self.right = None
        
        class Solution:
            # @param {TreeNode} root
            # @param {integer} k
            # @return {integer}
            def kthSmallest(self, root, k):
                if root.left==None and root.right==None:
                    return root.val
                lowest=root
                leftseq=[root]
                while lowest.left!=None:
                    lowest=lowest.left
                    leftseq.append(lowest)
                leftseq.pop()
                while k>1:
                    if lowest.right==None:
                        lowest=leftseq.pop()
                    else:
                        lowest=lowest.right
                        while lowest.left!=None:
                            leftseq.append(lowest)
                            lowest=lowest.left
                    k-=1
                return lowest.val

Log in to reply
 

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