Last active
August 5, 2016 15:46
-
-
Save jay3686/0735bda62e0c4ed90706976e532fcc1e to your computer and use it in GitHub Desktop.
kth element in tree in order traversal
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
### | |
# 5 | |
# 3 7 | |
# 2 4 6 8 | |
# 1 | |
### | |
# n c k | |
# 1 0 1 | |
# 2 1 2 1 | |
# 3 2 3 2 1 | |
# 4 3 4 3 2 1 | |
# 5 4 5 4 3 2 1 | |
# 6 5 6 5 4 3 2 1 | |
def in_order_k(tree_node, k): | |
if k < 0: | |
return k - 1 | |
if tree_node.left: | |
k = in_order_k(tree_node.left, k) | |
if k - 1 == 0: | |
print tree_node.value | |
return k - 1 | |
if tree_node.right: | |
k = in_order_k(tree_node.right, k - 1) | |
return k - 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment