Skip to content

Instantly share code, notes, and snippets.

@izeigerman
Created May 22, 2019 03:49
Show Gist options
  • Save izeigerman/aaceb48b376774d4d0a11446d3756338 to your computer and use it in GitHub Desktop.
Save izeigerman/aaceb48b376774d4d0a11446d3756338 to your computer and use it in GitHub Desktop.
# 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