美文网首页
固定和 二叉树版

固定和 二叉树版

作者: 锦绣拾年 | 来源:发表于2020-02-14 22:15 被阅读0次

也是用递归,返回树根
加左子树的结果
加右子树的结果
符合就push进去vector

题目

给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。

说明: 叶子节点是指没有子节点的节点。

示例:
给定如下二叉树,以及目标和 sum = 22,

              5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1
返回:

[
   [5,4,11,2],
   [5,8,4,5]
]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/path-sum-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

/**
 * 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:
    void path(TreeNode* root, int nsum,int sum,vector<vector<int>>&res,vector<int>&now){
        //这个不确定是不是都是>0
        if(root==NULL)
            return;
        if(root->left||root->right){
            //计算子树
            now.push_back(root->val);
                            vector<int> tmp;
                for(int i=0;i<now.size();i++){
                    tmp.push_back(now[i]);
                }
            if(root->left)
                path(root->left,nsum+root->val,sum,res,now);
            if(root->right){

                path(root->right,nsum+root->val,sum,res,tmp);
            }
            return;
            
            
        }else{
            //子树没有
            if(root->val+nsum==sum){
                now.push_back(root->val);
                res.push_back(now);
            }
            return;
            
            
        }
        
        
    }
    
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<vector<int>> res;
        vector<int> now;
        path(root,0,sum,res,now);
        
        return res;
        
        
        
    }
};

后又参考某答题法更改
这里让我印象深刻的是,vector<int> a 被push到vector<vector<int>>里居然还可以更改,相当于push进去一个固定值而不是地址。

/**
 * 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:
    void path(TreeNode* root, int nsum,int sum,vector<vector<int>>&res,vector<int>&now){
        //这个不确定是不是都是>0
        if(root==NULL)
            return;
        if(root->left||root->right){
            //计算子树
            
                //             vector<int> tmp;
                // for(int i=0;i<now.size();i++){
                //     tmp.push_back(now[i]);
                // }
            if(root->left){
                now.push_back(root->val);
                 path(root->left,nsum+root->val,sum,res,now);
                 now.pop_back();
            }
               
            if(root->right){
                now.push_back(root->val);
                path(root->right,nsum+root->val,sum,res,now);
                now.pop_back();
            }
            return;
            
            
        }else{
            //子树没有
            if(root->val+nsum==sum){
                now.push_back(root->val);
                res.push_back(now);
                now.pop_back();
            }
            return;
            
            
        }
        
        
    }
    
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<vector<int>> res;
        vector<int> now;
        path(root,0,sum,res,now);
        
        return res;
        
        
        
    }
};

参考答案

/*
 * 先序遍历 回溯
 */
class Solution {
public:
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<vector<int>> ret;
        if(!root) return ret;
        vector<int> curr;   // 当前路径
        hasPathSum(root, sum, curr, ret);
        return ret;
    }

private:
    void hasPathSum(TreeNode* root, int sum, vector<int>& curr, vector<vector<int>>& ret) {
        sum -= root->val;
        curr.push_back(root->val);
        if(sum == 0 && !root->left && !root->right) {
            ret.push_back(curr);    // 满足条件打印
            return ;
        }
        if(root->left){
            hasPathSum(root->left, sum,curr,ret);
            curr.pop_back();        // 回溯
        }
        if(root->right){
            hasPathSum(root->right,sum,curr,ret);
            curr.pop_back();        // 回溯
        }
    }
};

作者:zhengjingwei
链接:https://leetcode-cn.com/problems/path-sum-ii/solution/hui-su-c-chao-guo-99-by-zhengjingwei/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

相关文章

网友评论

      本文标题:固定和 二叉树版

      本文链接:https://www.haomeiwen.com/subject/kfksfhtx.html