列表

详情


NC8. 二叉树中和为某一值的路径(二)

描述

输入一颗二叉树的根节点root和一个整数expectNumber,找出二叉树中结点值的和为expectNumber的所有路径。
1.该题路径定义为从树的根结点开始往下一直到叶子结点所经过的结点
2.叶子节点是指没有子节点的节点
3.路径只能从父节点到子节点,不能从子节点到父节点
4.总节点数目为n

如二叉树root为{10,5,12,4,7},expectNumber为22
则合法路径有[[10,5,7],[10,12]]

数据范围:
树中节点总数在范围 [0, 5000] 内
-1000 <= 节点值 <= 1000
-1000 <= expectNumber <= 1000

示例1

输入:

{10,5,12,4,7},22

输出:

[[10,5,7],[10,12]]

说明:

返回[[10,12],[10,5,7]]也是对的

示例2

输入:

{10,5,12,4,7},15

输出:

[]

示例3

输入:

{2,3},0

输出:

[]

示例4

输入:

{1,3,4},7

输出:

[]

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 2ms, 内存消耗: 548KB, 提交时间: 2022-02-09

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    vector<vector<int>> FindPath(TreeNode* root,int expectNumber) {
        vector<vector<int> > res;
        if (root == NULL) {
            return res;
        }
        helper(root, expectNumber, res);
        return res;
        
    }
    
    void helper(TreeNode* root, int target, vector<vector<int> >& res) {
        if (root != NULL) {
            tmp.push_back(root->val);
        } else {
            return;
        }
        if (root->left == NULL && root->right == NULL && target == root->val) {
            res.push_back(tmp);
        } else {
            helper(root->left, target-root->val, res);
     
            helper(root->right, target-root->val, res);
        }
        
        tmp.pop_back();//这一步一定得有,弹出不符合要求的值
    }
private:
    vector<int> tmp;
};

C++ 解法, 执行用时: 2ms, 内存消耗: 560KB, 提交时间: 2021-10-17

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    vector<vector<int> > result;
    void compare(vector<int>& path,TreeNode *root,int expectNumber,int sum)
    {
        sum += root->val;
        if(sum == expectNumber && root->left == NULL && root->right == NULL)
        {
            result.push_back(path);
            return ;
        }
        
        if(root->left)
        {
            path.push_back(root->left->val);
            compare(path,root->left,expectNumber,sum);
            path.pop_back();             //没达到目标,就弹出
            
        }
        if(root->right)
        {
            path.push_back(root->right->val);
            compare(path,root->right,expectNumber,sum);
            path.pop_back();             //没达到目标就弹出
            
        }
    }
    
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        if(root == NULL)
        {
            return result;
        }
        vector<int> path;
        path.push_back(root->val);
        compare(path,root,expectNumber,0);
        return result;

    }
};

C++ 解法, 执行用时: 2ms, 内存消耗: 572KB, 提交时间: 2022-02-08

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    vector<vector<int>> ans;
    vector<int> path;
    vector<vector<int>> FindPath(TreeNode* root,int expectNumber) {
        dfs(root, 0, expectNumber);
        return ans;
    }
    void dfs(TreeNode* root, int sum, int target){
        if(!root) return;
        path.push_back(root -> val);
        sum += root -> val;
        if(!root -> left && !root -> right){
            if(sum == target) ans.push_back(path);
        } else {
            if(root -> left) dfs(root -> left, sum, target);
            if(root -> right) dfs(root -> right, sum, target);
        }
        
        path.pop_back();
    }
};

C++ 解法, 执行用时: 2ms, 内存消耗: 576KB, 提交时间: 2021-10-07

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    vector<vector<int> > res;
    void bc(vector<int>& path,TreeNode* root,int expectNumber,int sum){
        sum+=root->val;
        if(sum==expectNumber&&root->left==nullptr&&root->right==nullptr){
            res.push_back(path);
            return;
        }
        if(root->left){
            path.push_back(root->left->val);
            bc(path,root->left,expectNumber,sum);
            path.pop_back();
        }
        if(root->right){
            path.push_back(root->right->val);
            bc(path,root->right,expectNumber,sum);
            path.pop_back();
        }
    }
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        if(root==nullptr)return res;
        vector<int> path;
        path.push_back(root->val);
        bc(path,root,expectNumber,0);
        return res;
    }
};

C++ 解法, 执行用时: 2ms, 内存消耗: 608KB, 提交时间: 2022-02-09

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    vector<vector<int>> FindPath(TreeNode* root,int expectNumber) {
         vector<int> path;
         vector<vector<int> > paths;
         FindPathCore(root,expectNumber,path,paths);
        return paths;
        
    }
    void FindPathCore(TreeNode* root,int ex, vector<int> &path,vector<vector<int> >& paths){
        if(!root) return;
        path.push_back(root->val);
        ex -= root->val;
        bool isLeaf =  root->left == NULL && root->right == NULL;
        if( isLeaf&& ex == 0 ){
            paths.push_back(path);
        }
        if(root->left){
            FindPathCore(root->left,ex,path,paths);
        }
        if(root->right){
            FindPathCore(root->right,ex,path,paths);
        }
        path.pop_back();
    }
};

上一题