二叉树为某一值的路径

题目:输入一颗二叉树的根节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)

思路:

深度优先搜索。使用前序遍历,使用两个全局变量result和tmp,result来存放最终结果,tmp用来存放临时结果。

每次遍历,我们先把root的值压入tmp,然后判断当前root是否同时满足:

  • 与给定数值相减为0;
  • 左子树为空;
  • 右子树为空。

如果满足条件,就将tmp压入result中,否则,依次遍历左右子树。需要注意的是,遍历左右子树的时候,全局变量tmp是不清空的,直到到了根结点才请空tmp。

/*
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;
        vector<int> temp;
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) 
    {
       
        if(root==NULL)
            return result;
        temp.push_back(root->val);
        if((expectNumber-root->val==0)&&(root->left==NULL)&&(root->right==NULL))
            result.push_back(temp);
        FindPath(root->left,expectNumber-root->val);  //遍历左子树
        FindPath(root->right,expectNumber-root->val);  //遍历右子树
        temp.pop_back();  //
        return result;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_42209189/article/details/81077729
今日推荐