513 C++ easy understand ! dfs && vector


  • 0
    N

    by dfs to travel every deep , just put first value of each deep , and just output last value is ok

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<int> result; //515 513
        int findBottomLeftValue(TreeNode* root) {
            help_dfs_513(root,0);
    
            return result[result.size()-1];
        }
        
        void help_dfs_513(TreeNode *root , int deep){
        if(!root) return ;
        if(result.size()<deep+1){
            result.push_back(root->val);
        }
    
         help_dfs_513(root->left,deep+1);
         help_dfs_513(root->right,deep+1);
         
             
    
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.