难度: Medium
原题连接
内容描述
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.
思路 1 - 时间复杂度: O(N)- 空间复杂度: O(N)******
class Solution:
def findBottomLeftValue(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return []
res, cur_level = [], [root]
while cur_level:
next_level, tmp_res = [], []
for node in cur_level:
tmp_res.append(node.val)
if node.left:
next_level.append(node.left)
if node.right:
next_level.append(node.right)
res.append(tmp_res)
cur_level = next_level
return res[-1][0]