Created
May 22, 2019 03:49
-
-
Save izeigerman/aaceb48b376774d4d0a11446d3756338 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for a binary tree node. | |
# class TreeNode: | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Solution: | |
def helper(self, root: TreeNode, k: int): | |
l = [] | |
if root.left is not None: | |
l.extend(self.helper(root.left, k)) | |
l.append(root.val) | |
if len(l) >= k: | |
return l | |
if root.right is not None: | |
l.extend(self.helper(root.right, k)) | |
return l | |
def kthSmallest(self, root: TreeNode, k: int) -> int: | |
return self.helper(root, k)[k - 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment