Binary tree OJ (two) the path I, II, III of a certain value in the binary tree

The path that sums to a certain value in the binary tree (1)

describe

Given a binary tree root and a value sum, determine whether there is a path whose sum of node values ​​from the root node to the leaf node is equal to sum.

1. The path of this question is defined as the node from the root node of the tree down to the leaf node

2. A leaf node refers to a node without child nodes

3. The path can only go from parent node to child node, not from child node to parent node

4. The total number of nodes is n


For example:
given the following binary tree, sum=22 sum=22,


Returns true because there is a path 5→4→11→25→4→11→2 and the sum of the node values ​​is 22

data range:

1. The number of nodes on the tree satisfies 0≤n≤100000≤n≤10000

2. The value of each node satisfies ∣val∣≤1000∣val∣≤1000

Requirements: space complexity O(n)O(n), time complexity O(n)O(n)

Advanced: Space complexity O (height of tree) O (height of tree), time complexity O(n)O(n)

[Solution 1] dfs

class Solution {
public:
    bool flag = false;
    void dfs(TreeNode* root, int sum)
    {
        if(root==nullptr)return;
        sum-=root->val;
        if(sum==0 && root->left==nullptr && root->right==nullptr)
        {
            flag = true;
            return;
        }
        dfs(root->left, sum);
        dfs(root->right, sum);
    }
    bool hasPathSum(TreeNode* root, int sum) {
        dfs(root, sum);
        return flag;
    }
};

 The path whose sum is a certain value in the binary tree (2)

describe

Enter the root node root of a binary tree and an integer expectNumber, and find all paths whose sum of node values ​​in the binary tree is expectNumber.

1. The path of this question is defined as the node from the root node of the tree down to the leaf node

2. A leaf node refers to a node without child nodes

3. The path can only go from parent node to child node, not from child node to parent node

4. The total number of nodes is n

Example 1

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

Return value: [[10,5,7],[10,12]] Explanation: Returning [[10,12],[10,5,7]] is also correct

class Solution {
public:
	vector<vector<int>>res;
	vector<int> temp;
	void dfs(TreeNode* root, int sum)
	{
		if(root==nullptr)return;
		sum-=root->val;
		temp.push_back(root->val);
		if(sum==0 && root->left==nullptr && root->right==nullptr)
		{
			res.push_back(temp);
		}
		dfs(root->left, sum);
		dfs(root->right, sum);
		sum+=root->val;    // 这里进行回溯还原
		temp.pop_back();
	}
    vector<vector<int>> FindPath(TreeNode* root,int expectNumber) {
        if(root == nullptr)return res;
		dfs(root, expectNumber);
		return res;
    }
};

The path whose sum is a certain value in the binary tree (3)

describe

Given a binary tree root and an integer value sum, find the number of paths in the tree whose sum of node values ​​is equal to sum.

1. The path definition of this question does not need to start from the root node, nor does it need to end at the leaf node, but it must be from the parent node down to the child node

2. The total number of nodes is n

3. Ensure that the number of paths returned at the end is within the integer range (that is, the number of paths is less than 231-1)

[Solution 1] Two layers of dfs nesting

class Solution {
public:
    int res = 0;
    void dfs(TreeNode* root, int sum)
    {
        if(root == nullptr)
            return;
        sum-=root->val;
        if(sum==0)  // 注意这里,后续可能还会有中和的现象,所以不能直接剪枝
            res++;
        dfs(root->left, sum);
        dfs(root->right, sum);
        sum+=root->val;
    }
    int FindPath(TreeNode* root, int sum) {
        // write code here
        if(root==nullptr)return 0;
        dfs(root, sum);
        FindPath(root->left, sum);
        FindPath(root->right, sum);
        return res;
    }
};

Guess you like

Origin blog.csdn.net/weixin_66151870/article/details/129113629