-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path129_SumRootToLeafNumbers.py
45 lines (38 loc) · 1.09 KB
/
129_SumRootToLeafNumbers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
"""
Depth First Search
"""
def sumNumbers(self, root):
node_stack = []
path_sum = 0
# Keep the path number from root to the current node.
cur_node_num = 0
while root or node_stack:
if root:
cur_node_num = cur_node_num * 10 + root.val
node_stack.append([root, cur_node_num])
root = root.left
else:
if node_stack:
pop_record = node_stack.pop()
root = pop_record[0].right
cur_node_num = pop_record[1]
# Meet a leaf node
if not pop_record[0].left and not root:
path_sum += cur_node_num
else:
break
return path_sum
"""
[]
[1,2,3]
[1,null,2,3,null,null,4,5,6]
"""