【LeetCode】112,113,437. Path Sum I, II, III
来源:互联网 发布:杭州软件开发公司 编辑:程序博客网 时间:2024/06/06 13:15
题目描述
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.
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
思路
递归遍历,判断从根节点到叶节点的和是否等于sum,是则返回true,否则返回false。
并且,只要有一个为true,则结果为true。(或运算)
特殊情况:空树返回false。
代码
class Solution {public: bool hasPathSum(TreeNode* root, int sum) { if (root == NULL) return false; if (root->left == NULL && root->right == NULL && root->val == sum) 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
思路
首先,我们需要一个vector二维数组来保存符合条件的一维路径数组。
判断从根节点到叶节点的和是否等于sum,路径可能不止一条,所以必须要把整棵树遍历。与上题类似,递归实现。不同的是,在这道题中,我们需要保存符合条件的路径。所以,在每次调用递归函数时,都需要将当前访问过的路径作为参数传递下去。当访问到叶节点时,如果和等于sum,则将路径存入目标二维数组中。
同样的,空树返回NULL。
代码
class Solution {public: vector<vector<int>> pathSum(TreeNode* root, int sum) { vector<vector<int>> result; vector<int> path; dfs(result, path, root, sum); return result; } void dfs(vector<vector<int>>& result, vector<int> path, TreeNode* root, int des) { if (root == NULL) return; path.push_back(root->val); if (root->left == NULL && root->right == NULL && root->val == des) result.push_back(path); else { dfs(result, path, root->left, des-root->val); dfs(result, path, root->right, des-root->val); } }};
题目描述
437-Path Sum III
You are given a binary tree in which each node contains an integer value.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.Example:
思路
每一个节点都可以作为路径的起点。所以,使用了两个递归函数:pathSum是求从所有节点出发的满足条件的路径数的和;dfs是寻找以某个节点为根节点的满足条件的路径的数量。
还有一点就是,与上面两道题不同,这一道题的路径并不需要从根节点到叶节点。也就是说,在从根节点遍历到叶节点的过程中,只要符合条件,就将结果加1然后继续遍历下去。
代码
class Solution {public: int pathSum(TreeNode* root, int sum) { if (root == NULL) return 0; return dfs(root, sum) + pathSum(root->left, sum) + pathSum(root->right, sum); } int dfs(TreeNode* root, int des) { if (root == NULL) return 0; return (root->val == des) + dfs(root->left, des-root->val) + dfs(root->right, des-root->val); }};
- 【LeetCode】112,113,437. Path Sum I, II, III
- [Leetcode] Path Sum I,II,III
- Leetcode--Path Sum I,II,III
- LeetCode 112&113 --Path Sum I & II;
- Path Sum I II III
- LeetCode 112, 123. Path Sum i, ii
- LeetCode:Path Sum I &&II
- 【LeetCode】path-sum i&ii
- LeetCode 437. Path Sum III
- LeetCode 437. Path Sum III
- LeetCode 437. Path Sum III
- leetcode 437. Path Sum III
- [LeetCode]437. Path Sum III
- 【LeetCode】 437. Path Sum III
- [leetcode]437. Path Sum III
- LeetCode 437. Path Sum III
- [leetcode]437. Path Sum III
- [LeetCode]437. Path Sum III
- 1.2、声明值和变量
- easyui tree 根据后台返回的数据属性的值控制checkbox的选中状态(可不可用)
- 谷歌Gson简单测试
- ISIS报文格式—通用包头
- Django 使用 bootstrap-table插件,表格分页 + UTC、时间戳 互转
- 【LeetCode】112,113,437. Path Sum I, II, III
- varchar2改为clob的解决步骤
- 用当前日期作为log文件名字
- navicat for mysql
- 电子合同干货大放送:数字签名的证据效力
- JAVA发展史
- ssh各自的作用
- RecyclerView添加头部
- poj1611 The Suspects