Leetcode中几道二叉树题 (IV)

四、树上的路径和

题目一: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.

思路:找到一个满足sum的path即可。

class Solution {
public:
    bool hasPathSum(TreeNode *root, int sum) {
        if(root==NULL) return false;
        return hasPathSum(root, sum, 0);
    }
    
    bool hasPathSum(TreeNode *root, int sum, int cur){
        if(root->left==NULL && root->right==NULL){   //叶子节点
            if(cur+root->val==sum) return true;
            return false;
        }
            
        int flag=false;
        if(root->left &&  hasPathSum(root->left, sum, cur+root->val))
    	    flag=true;
        if(root->right &&  hasPathSum(root->right, sum, cur+root->val))
        	flag=true;
            
        return flag;
    }
};


题目二:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum

思路:要求枚举出所有的满足要求的路径

class Solution {
public:
    
    void hasPathSum(vector<vector<int> >& res, vector<int>& path, TreeNode *root, int sum, int cur){
        if(root->left==NULL && root->right==NULL){
            if(cur+root->val==sum){
                path.push_back(root->val);
                res.push_back(path);
                path.pop_back();
            } 
            return;
        }
        
        if(root->left){
            path.push_back(root->val);
            hasPathSum(res, path, root->left, sum, cur+root->val); 
            path.pop_back();  //恢复原状
        }
    
        if(root->right){
            path.push_back(root->val);
            hasPathSum(res, path, root->right, sum, cur+root->val);
            path.pop_back(); <span style="font-family: 'Comic Sans MS';">//恢复原状</span>
        }
    }
    
    vector<vector<int> > pathSum(TreeNode *root, int sum) {
        vector<vector<int> > res;
        vector<int> path;
        if(root==NULL) return res;
        hasPathSum(res, path, root, sum, 0);
        return res;
    }
};

题目三: Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree.

思路:因为起点和终点随意,故只要在同一棵树上的任意两点的任意都存在一条路径。最大路径和可以单在左、右子树上,或者横跨左右子树。

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxPathSum(TreeNode *root) {
        int max_val=-1<<12;
        maxPathSum(max_val, root);
        return max_val;
    }
    
    int maxPathSum(int& max_val, TreeNode* root){  //通过引用返回最大sum
        if(root==NULL) return 0;
        int local_max=root->val;
        
        int left=maxPathSum(max_val, root->left);
        int right=maxPathSum(max_val, root->right);
        
        if(left>0) local_max+=left;   //左子树最大和
        if(right>0) local_max+=right; //左右子树最大和
        
        if(max_val<local_max) max_val=local_max;
        return  root->val+ max(max(0,left), right); //子树最大和
    }
    
};
说明:因为要求最大sum,故负数除外!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值