Leetcode中几道二叉树题 (IV)

来源:互联网 发布:火影忍者手游外卦软件 编辑:程序博客网 时间:2024/06/04 23:12

四、树上的路径和

题目一: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,故负数除外!

0 0
原创粉丝点击