[牛客网-Leetcode] #树zhongpath-sum-ii

Path and ii path-sum-ii

Title description

Given a binary tree and a value sum, please find the path where the sum of the node values ​​from the root node to the leaf node is equal to sum,
for example:
Given the following binary tree, sum=22,
5↵             / ↵            4   8↵           /   / ↵          11  13  4↵         /      / ↵        7    2  5   1
return
[↵ [5,4,11] ,2],↵ [5,8,4,5]↵]↵

Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.
For example:
Given the below binary tree andsum = 22,
5↵             / ↵            4   8↵           /   / ↵          11  13  4↵         /      / ↵        7    2  5   1↵
return

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

Problem-solving ideas

Backtracking

  • Path: recorded in the track
    Selection list: left and right subtrees that are not empty
    End condition: reach the leaf node and meet the conditions
  • Note: Since you need to start from the subtree of the root node, you must first judge the root node separately, and then backtrack
/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 * };
 */

class Solution {
    
    
public:
    vector<vector<int> > pathSum(TreeNode* root, int sum) {
    
    
        vector<vector<int> > res;
        vector<int> track;
        /*由于要从根节点的子树开始判断,所以要先单独对根节点进行判断*/
        if(root == NULL) return res;   //根为空,直接返回

        //先将根节点添加进路径中
        track.push_back(root -> val);
        backtrack(root, sum, track, res);
        return res;
    }
    void backtrack(TreeNode* root, int& sum, vector<int>& track, vector<vector<int> >& res) {
    
    
        if(root == NULL) return ;
        if(root -> left == NULL && root -> right == NULL && sum - root -> val == 0) {
    
    
            res.push_back(track);
            return ;
        }
        //两个选择,只要子树不空就做选择
        if(root -> left != NULL) {
    
    
            sum -= root -> val;
            //添加左子树
            track.push_back(root -> left -> val);
            backtrack(root -> left, sum, track, res);
            sum += root -> val;
            track.pop_back();
        }
        if(root -> right != NULL) {
    
    
            sum -= root -> val;
            //添加右子树
            track.push_back(root -> right -> val);
            backtrack(root -> right, sum, track, res);
            sum += root -> val;
            track.pop_back();
        }
    }
};

DFS

/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 * };
 */

class Solution {
    
    
public:
    vector<vector<int> > pathSum(TreeNode* root, int sum) {
    
    
        vector<vector<int> > res;
        vector<int> track;
        if(root == NULL) return res;
        
        dfs(root, sum, track, res);
        return res;
    }
    void dfs(TreeNode* root, int sum, vector<int> track, vector<vector<int> >& res) {
    
    
        if(root == NULL) return ;
        track.push_back(root -> val);
        if(root -> left == NULL && root -> right == NULL && sum - root -> val == 0) {
    
    
            res.push_back(track);
        }
        //进入下一层
        if(root -> left != NULL) {
    
    
            dfs(root -> left, sum - root -> val, track, res);
        }
        if(root -> right != NULL) {
    
    
            dfs(root -> right, sum - root -> val, track, res);
        }
    }
};

Guess you like

Origin blog.csdn.net/cys975900334/article/details/106824622