aboutsummaryrefslogtreecommitdiff
path: root/leetcode.py
blob: 6dcb2750c17e9b6f677ed4e3ce5c04733c717ed3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


def list_to_tree(input):
    if not input:
        return None
    root = TreeNode(input[0])
    q, i = [root], 1
    while i < len(input):
        curr = q.pop(0)
        if input[i] is not None:
            curr.left = TreeNode(input[i])
            q.append(curr.left)
        i += 1
        if i < len(input) and input[i] is not None:
            curr.right = TreeNode(input[i])
            q.append(curr.right)
        i += 1
    return root