题解 | #二叉树中和为某一值的路径(二)#

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

https://www.nowcoder.com/practice/b736e784e3e34731af99065031301bca


class Solution {
public:
	vector<vector<int> > res;

	void find(TreeNode* p,vector<int> rec,int target){
		vector<int> record;
		for(auto x : rec) record.push_back(x);
		record.push_back(p->val);
		if(p->left != NULL) find(p->left,record,target);
		if(p->right != NULL) find(p->right,record,target);
		if(p->left == NULL && p->right == NULL){
			int sum = 0;
			for(auto x : record) sum += x;
			if(sum == target) res.push_back(record);
		}
		return;
	}

    vector<vector<int>> FindPath(TreeNode* root,int expectNumber) {
        vector<int> temp;
		if(root == NULL) return {};
		temp.push_back(root->val);
		if(root->val == expectNumber && root->left==NULL && root->right == NULL) res.push_back(temp);
		if(root->left != NULL) find(root->left,temp,expectNumber);
		if(root->right != NULL) find(root->right,temp,expectNumber);
		return res;
    }
};

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 18:05
哈哈哈哈哈感觉朋友找工作的已经疯掉了,直接上图
码农索隆:真老板娘:“我嘞个去,这不我当年的套路吗
点赞 评论 收藏
分享
05-23 19:02
吉林大学 Java
点赞 评论 收藏
分享
这不纯纯作弊了吗😢😢😢
编程界菜鸡:信这个的这辈子有了,这智商你靠啥都没用
你找工作的时候用AI吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务