513. Find Bottom Left Tree Value

来源:互联网 发布:adc0808数据手册 编辑:程序博客网 时间:2024/06/06 01:46
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.

class Solution {public:    int findBottomLeftValue(TreeNode* root) {                vector<int> res(2, 0);        return helper(root, 1, res);    }        int helper(TreeNode* root, int depth, vector<int>& res){        if(depth>res[1]){res[0]=root->val; res[1]=depth;}        if(root->left) helper(root->left, depth+1, res);        if(root->right) helper(root->right, depth+1, res);                return res[0];    }    };


0 0