二叉樹(shù)的遞歸求最大和問(wèn)題

**112. Path Sum **
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22,

              5
             / \
            4   8
           /   / \
          11  13  4
         /  \      \
        7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
代碼如下:

class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) {
        if(root==NULL)
          return false;
        if(root->val==sum&&root->left==NULL&&root->right==NULL) //正好是這個(gè)節(jié)點(diǎn),且為最底層的節(jié)點(diǎn)(左右子樹(shù)為空)
          return true;
        else
          return hasPathSum(root->left,sum - root->val)||hasPathSum(root->right,sum - root->val);
    }
};

**113. Path Sum II **
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 and sum = 22,

              5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1

return

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

代碼如下:

class Solution {
public:
    
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<vector<int>> rec;
        vector<int> temp;
        if(root==NULL)
          return rec;
        DFS(root,sum,rec,temp);
        return rec;
    }
    void DFS(TreeNode* root,int sum,vector<vector<int>> &rec,vector<int> &temp)
    {
        if(root==NULL)
          return;
        temp.push_back(root->val);//注意:這里壓入
        if(root->val==sum&&root->left==NULL&&root->right==NULL)
            rec.push_back(temp);
            
        if(root->left)
          DFS(root->left,sum - root->val,rec,temp);
        if(root->right)
          DFS(root->right,sum - root->val,rec,temp);
        temp.pop_back();//注意:這里彈出
    }
};

**129. Sum Root to Leaf Numbers **
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
For example,

    1
   / \
  2   3

The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Return the sum = 12 + 13 = 25.
代碼如下:

class Solution {
public:
    int sumNumbers(TreeNode* root) {
        return dfs(root,0);
    }
    int dfs(TreeNode* root,int sum)
    {
        if(root==NULL)
          return 0;
        if(root->left==NULL&&root->right==NULL)
          return 10*sum + root->val;
        else
          return dfs(root->left,10*sum + root->val) + dfs(root->right,10*sum + root->val);
    }
};

**124. Binary Tree Maximum Path Sum **
Given a binary tree, find the maximum path sum.

For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.

For example:
Given the below binary tree,

       1
      / \
     2   3

Return 6.
代碼如下:

class Solution {
public:
    int sum = INT_MIN;
    int maxPathSum(TreeNode* root) {
        if(root==NULL)
          return 0;
        mSum(root);
        return sum;
    }
    int mSum(TreeNode* root)
    {
        if(root==NULL)
          return 0;
        int left = mSum(root->left);
        int right = mSum(root->right);
        int current = max(max(left+root->val,right+root->val),root->val);
        int result = max(current,left+right+root->val);
        sum = max(sum,result);
        return current;
    }
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容