Daily Leetcode 513. Find Bottom Left Tree Value
https://leetcode.com/problems/find-bottom-left-tree-value/
Medium
问题描述:
Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Note: You may assume the tree (i.e., the given root node) is not NULL.
题目分析:
所谓leftmost
,就是要找到给定二叉树最深层次、位置在最左边的那个节点,二叉树相关的问题基本上都可以转换成树的遍历的问题。我们可以以先序顺序遍历给定二叉树,在遍历过程中,更新最大深度和对应的最大值,只有当前节点的深度大于遍历至今保存的最大深度时,才进行上述更新操作。
代码:
1 | # Definition for a binary tree node. |