Zusammenfassung des LeetCode-Pfads III

Zusammenfassung des LeetCode-Pfads III

Thema

44 Pfadsumme III

Autor: Turbo Zeitlimit: 1S Kapitel: DS:Tree

Einreichen nach: 04.08.2022 23:55:00 und der Punktemultiplikator beträgt 50 %

Problembeschreibung :

Bei einem gegebenen Binärbaum speichert jeder Knoten einen ganzzahligen Wert.

Finden Sie die Pfadsumme, die der Gesamtzahl der angegebenen Pfade entspricht.

Der Pfad muss nicht am Wurzelknoten beginnen und auch nicht an einem Blattknoten enden, sondern die Pfadrichtung muss nach unten gerichtet sein (nur vom übergeordneten Knoten zum untergeordneten Knoten).

Der Binärbaum hat nicht mehr als 1000 Knoten und der Knotenwertbereich ist eine Ganzzahl von [-1000000,1000000].

Beispiel:

Wurzel = [10,5,-3,3,2,null,11,3,-2,null,1], Summe = 8

  10

 /  \

5   -3

/ \ \

3 2 11

/ \ \

3 -2 1

Rückkehr 3. Pfade, deren Summe 8 beträgt, sind:

  1. 5 -> 3

  2. 5 -> 2 -> 1

  3. -3 -> 11

Folgende Hauptfunktion kann genutzt werden:

#enthalten

#enthalten

#enthalten

#enthalten

#enthalten

Verwenden des Namensraums std;

Struktur TreeNode

{

int val;

TreeNode *left;

TreeNode *right;

TreeNode() : val(0), left(NULL), right(NULL) {}

TreeNode(int x) : val(x), left(NULL), right(NULL) {}

TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}

};

TreeNode* inputTree()

{

int n,count=0;

char item[100];

cin>>n;

if (n==0)

    return NULL;

cin>>item;

TreeNode* root = new TreeNode(atoi(item));

count++;

queue<TreeNode*> nodeQueue;

nodeQueue.push(root);
while (count<n)
{

    TreeNode* node = nodeQueue.front();

    nodeQueue.pop();

    cin>>item;

    count++;

    if (strcmp(item,"null")!=0)

    {

        int leftNumber = atoi(item);

        node->left = new TreeNode(leftNumber);

        nodeQueue.push(node->left);

    }

    if (count==n)

        break;

    cin>>item;

    count++;

    if (strcmp(item,"null")!=0)

    {

        int rightNumber = atoi(item);

        node->right = new TreeNode(rightNumber);

        nodeQueue.push(node->right);

    }

}

return root;

}

int main()

{

TreeNode* root;

root=inputTree();

int sum;

cin>>sum;

int res=Solution().pathSum(root,sum);

cout<<res<<endl;

}

Code


```cpp

```cpp

```cpp
#include <iostream>

#include <queue>

#include <cstdlib>

#include <cstring>

#include<map>

using namespace std;

struct TreeNode

{
    
    

    int val;

    TreeNode *left;

    TreeNode *right;

    TreeNode() : val(0), left(NULL), right(NULL) {
    
    }

    TreeNode(int x) : val(x), left(NULL), right(NULL) {
    
    }

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {
    
    }

};

TreeNode* inputTree()

{
    
    

    int n,count=0;

    char item[100];

    cin>>n;

    if (n==0)

        return NULL;

    cin>>item;

    TreeNode* root = new TreeNode(atoi(item));

    count++;

    queue<TreeNode*> nodeQueue;

    nodeQueue.push(root);

    while (count<n)

    {
    
    

        TreeNode* node = nodeQueue.front();

        nodeQueue.pop();

        cin>>item;

        count++;

        if (strcmp(item,"null")!=0)

        {
    
    

            int leftNumber = atoi(item);

            node->left = new TreeNode(leftNumber);

            nodeQueue.push(node->left);

        }

        if (count==n)

            break;

        cin>>item;

        count++;

        if (strcmp(item,"null")!=0)

        {
    
    

            int rightNumber = atoi(item);

            node->right = new TreeNode(rightNumber);

            nodeQueue.push(node->right);

        }

    }

    return root;

}


class Solution {
    
    
public:
    int pathSum(TreeNode* root, int targetSum) {
    
    
          if(root== nullptr)
              return 0;
        return pathSum(root->left,targetSum)+ pathSum(root->right,targetSum)+  pathSumStartWithRoot(root,targetSum);
        //纯左+纯右+带根节点
    }
    int pathSumStartWithRoot(TreeNode*root,int targetSum){
    
    
        if(!root){
    
    
            return 0;
        }
        int count =0 ;
        if(root->val==targetSum)
            count++;
        count+= pathSumStartWithRoot(root->left,targetSum-root->val);
        count+= pathSumStartWithRoot(root->right,targetSum-root->val);
        return count;
    }
};
int main()

{
    
    

    TreeNode* root;

    root=inputTree();

    int sum;

    cin>>sum;

    int res=Solution().pathSum(root,sum);

    cout<<res<<endl;

}

Supongo que te gusta

Origin blog.csdn.net/qq_43801927/article/details/126151678
Recomendado
Clasificación