Binary tree and path for a certain value - byte jump written test programming C/C++

Posted by ziltech on Tue, 04 Jan 2022 15:25:56 +0100

Title Description

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

1. The problem path is defined as the node from the root node of the tree to the leaf node

(cat bin roars and prompts: pay great attention here - you must go to the leaf node until the leaf node has no child nodes! Pay attention to example 3)

2. Leaf nodes refer to nodes without child nodes

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

4. The total number of nodes is n

(for more blog posts, welcome to my blog to learn and communicate have_a_cat blog_ CSDN blog - PHP, DCAT admin framework, blogger in the field of popular written interview in large factories)

Data range:

The total number of nodes in the tree is in the range [0, 5000]

-1000 < = node value < = 1000

-1000 <= expectNumber <= 1000

Example

Example serial number

input

output

remarks

Example 1

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

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

It is also right to return [[10,12], [10,5,7]]

Example 2

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

[]

Example 3

{2,3},0

[]

Example 4

{1,3,4},7

[]

(for more blog posts, welcome to my blog to learn and communicate have_a_cat blog_ CSDN blog - PHP, DCAT admin framework, blogger in the field of popular written interview in large factories)

analysis

After reading the question once, the smell told you maobin that this is a tree level traversal problem.

Because there are keywords "path from root to leaf" and "find all paths"

Next, let's talk about the problem-solving ideas in detail (take example 1)

Hierarchical traversal of the tree (level 0)

① If the current node is not NULL, the value of the current node is added

(for more blog posts, welcome to my blog to learn and communicate have_a_cat blog_ CSDN blog - PHP, DCAT admin framework, blogger in the field of popular written interview in large factories)

Hierarchical traversal of the tree (layer 1)

② The left child node adds its own value to the current path of the parent node;

The right child node copies the current path of the parent node and adds its own value

③ When the current node is a leaf node (no child node) and the current path value is equal to expectNumber, the current path is added to the return value

Hierarchical traversal of the tree (layer 2)

The final return value is: [[10,12], [10,5,7]]

(for more blog posts, welcome to my blog to learn and communicate have_a_cat blog_ CSDN blog - PHP, DCAT admin framework, blogger in the field of popular written interview in large factories)

C + + code

According to the thought of ①, ② and ③, the code can be obtained:

/*
struct TreeNode {
              int val;
              struct TreeNode *left;
              struct TreeNode *right;
              TreeNode(int x) :val(x), left(NULL), right(NULL) {
              }
};*/

class Solution {
public:
    int findnum(TreeNode* root, int expectNumber,vector<vector<int>> *f, vector<int> *now, int num){
        if(root != NULL){/* ①If the current node is not NULL, the value of the current node is added */
            now->push_back(root->val);
            num += root->val;
            /* ③When the current node is a leaf node (no child node) and the current path value is equal to expectNumber, the current path is added to the return value*/
            if(root->left==NULL && root->right==NULL){
                if(num == expectNumber){
                    f->push_back(*now);
                    return 0;
                }
            }
            else{
                findnum(root->left, expectNumber, f, now, num);/* ②The left child node adds its own value to the current path of the parent node;*/
                vector<int> now_right(*now); /* ②The right child node copies the current path of the parent node and adds its own value*/
                findnum(root->right, expectNumber, f, &now_right, num);
            }
        }
        return 0;
    }

   
    vector<vector<int>> FindPath(TreeNode* root,int expectNumber) {
        vector<vector<int>> f;
        vector<int> now;
        int num = 0;
        findnum(root, expectNumber, &f, &now, num);
        return f;
    }
};

Step pit summary

Step pit serial number

input

output

summary

Step Pit 1

{2,-1,#,#,-5,7,0,#,-5,#,-1,#,-4,-5},-10

[[2,-1,-5,0,-1,-5]]

expectNumber may be negative

Here, I stepped on the smart pruning and wrote in tears:

The expected number may be negative. In the process, the sum of the current path may be temporarily greater than the expected number. Therefore, do not prune like this

Error:

(for more blog posts, welcome to my blog to learn and communicate have_a_cat blog_ CSDN blog - PHP, DCAT admin framework, blogger in the field of popular written interview in large factories)

Topics: C++ Interview Binary tree recursion