题目:
Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
Input:
2
/
1 3
Output:
1
Example 2:
Input:
1
/ \
2 3
/ / \
4 5 6
/
7
Output:
7
Note: You may assume the tree (i.e., the given root node) is not NULL.
我的写法:
q,ret=[root],[]
while any(q):
ret.append([i.val for i in q if i])
q=[child for node in q if node for child in [node.left,node.right]]
return ret[-1][0]
更简短的写法:
queue = [root]
for node in queue:
queue += filter(None, (node.right, node.left))
return node.val