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; }};
思路:要求枚举出所有的满足要求的路径
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
- Leetcode中几道二叉树题 (IV)
- Leetcode中几道二叉树题 I
- Leetcode中几道二叉树题 II
- Leetcode中几道二叉树题 III
- leetcode随笔IV
- 【Leetcode】Combination Sum IV
- [leetcode] Combination Sum IV
- Leetcode: Combination Sum IV
- LeetCode Combination Sum IV
- Leetcode Combination Sum IV
- LeetCode: Combination Sum IV
- leetcode[Two Sum IV
- LeetCode Two Sum IV
- leetcode Two Sum IV
- LeetCode--Combination Sum IV
- LeetCode二叉树基础题(不断更新)
- leetcode:构建二叉树
- 重建二叉树 leetcode
- NULL, 0, 空格与'0','\0'
- 第十二周 教师兼干部
- 定义符
- Ubuntu 12.04 LTS 添加国内源
- POI操作Excel的简单案例
- Leetcode中几道二叉树题 (IV)
- android中Invalidate和postInvalidate的区别
- Linux lftp测试问题
- 圆的比较
- Linux文件共享(六)——描述符传递
- [Bzoj1601][Usaco2008 Oct]灌水
- 第二学期第112周项目3--摩托车继承自行车和机动车
- 404页面设置
- Hibernate主键生成策略