Skip to content

Instantly share code, notes, and snippets.

@xudifsd
Created October 21, 2015 06:50
Show Gist options
  • Save xudifsd/3b4f8eae16208fc43344 to your computer and use it in GitHub Desktop.
Save xudifsd/3b4f8eae16208fc43344 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def printTreeWithDistance(root, k):
def impl(root, path):
if root == None:
if len(path) >= k:
path[-k] = True
return
path.append(False)
impl(root.left, path)
impl(root.right, path)
v = path.pop()
if v:
print root.val
impl(root, [])
if __name__ == '__main__':
root = TreeNode(10)
root.right = TreeNode(11)
root.left = TreeNode(9)
root.left.right = TreeNode(8)
root.left.left = TreeNode(7)
root.left.left.right = TreeNode(6)
root.left.left.left = TreeNode(5)
root.left.left.left.right = TreeNode(4)
printTreeWithDistance(root, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment