Skip to content

Instantly share code, notes, and snippets.

@dapangmao
Created March 13, 2015 13:05
Show Gist options
  • Save dapangmao/12586102c79dd8536263 to your computer and use it in GitHub Desktop.
Save dapangmao/12586102c79dd8536263 to your computer and use it in GitHub Desktop.
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