Find Bottom Left Tree Value

Tags : leetcode, tree, bfs, dfs, binary-tree, cpp, medium

Given the root of a binary tree, return the leftmost value in the last row of the tree.

Examples #

Example 1:

img

Input: root = [2,1,3]
Output: 1

Example 2:

img

Input: root = [1,2,3,4,null,5,6,null,null,7]
Output: 7

Constraints #

Solutions #

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int deepR,deepC,res;
    void helper(TreeNode* root, int r, int c){
        if(root==NULL) return;
        if(r>deepR) { deepR=r; deepC=c; res = root->val;}
        else if (r==deepR && c < deepC) {deepC=c; res = root->val;}
        helper(root->left,r+1,c-1);
        helper(root->right,r+1,c+1);
    }
    int findBottomLeftValue(TreeNode* root) {
        deepR=deepC=0;
        res = root->val;
        helper(root,deepR,deepC);
        return res;
    }
};