Created
March 13, 2015 13:05
-
-
Save dapangmao/12586102c79dd8536263 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class TreeNode: | |
def __init__(self, x): | |
self.val = x | |
self.left = None | |
self.right = None | |
input = [1, 2, 3, 4, '#', 5, 6, 7, 8, 9] | |
def map_node(x): | |
if x == '#': | |
return None | |
return TreeNode(x) | |
def load_nodes(input): | |
test = map(map_node, input) | |
carry = [test[0]] | |
i = 1 | |
while carry: | |
current = [] | |
for x in carry: | |
if i < len(test): | |
x.left = test[i] | |
if x.left: | |
current.append(x.left) | |
i += 1 | |
if i < len(test): | |
x.right = test[i] | |
if x.right: | |
current.append(x.right) | |
i += 1 | |
carry = current | |
return test[0] | |
root = load_nodes(input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment