问题描述:
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
解题思路:
从该题可以看出,用dfs最简单,在dfs递归中height<level,height=level.再加值,一直到root为空
class Solution {
public:
int ret=0,height=0;
int findBottomLeftValue(TreeNode* root) {
int level=1;
dfs(level,root);
return ret;
}
void dfs(int level,TreeNode* root)
{
if(root==NULL) return;
if(height<level)
{
ret=root->val;
height=level;
}
dfs(level+1,root->left);
dfs(level+1,root->right);
}
};