LeetCode OJ:Path Sum
来源:互联网 发布:淘宝用什么快递单打印 编辑:程序博客网 时间:2024/06/05 07:43
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.
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool dfs(TreeNode *root,int cur,int &sum){ if(root==NULL)return false; if(root->left==NULL&&root->right==NULL){ if(cur+root->val==sum)return true; else return false; } return dfs(root->left,cur+root->val,sum)||dfs(root->right,cur+root->val,sum); } bool hasPathSum(TreeNode *root, int sum) { if(root==NULL)return false; return dfs(root,0,sum); }};
0 0
- LeetCode OJ:Path Sum
- LeetCode OJ Path Sum
- LeetCode OJ:Path Sum II
- LeetCode OJ:Minimum Path Sum
- LeetCode OJ - Path Sum II
- LeetCode OJ - Minimum Path Sum
- LeetCode OJ Minimum Path Sum
- LeetCode OJ Path Sum II
- leetcode oj java Path Sum
- LeetCode OJ:Binary Tree Maximum Path Sum
- LeetCode OJ - Binary Tree Maximum Path Sum
- LeetCode OJ Binary Tree Maximum Path Sum
- <LeetCode OJ> 64. Minimum Path Sum
- LeetCode Oj 112. Path Sum 解题报告
- LeetCode OJ-64.Minimum Path Sum(DP)
- leetcode oj java 64. Minimum Path Sum
- leetcode Path Sum && Path Sum ||
- 【Leetcode】Path Sum (Sum)
- 关于ModelSim从quartus自动启动仿真
- DNSPod:国内所有通用顶级域的根恢复正常
- hinernate 注解配置timestamp类型
- 创建div
- Spiral Matrix II 螺旋矩阵II
- LeetCode OJ:Path Sum
- hdu 1875 最小生成树+并查集
- 1293. 3n+1数链问题
- Divide Two Integers 两个整数相除
- 我要开始记录。 从今天开始!!
- 用JS-appendChild实现学生信息录入
- HDU2112 HDU Today
- 十、短暂的春秋---与机会失之交臂的公司
- 圆检测——最小二乘法拟合圆的推导