[LeetCode] Path Sum
来源:互联网 发布:网络三大奇书四大神书 编辑:程序博客网 时间:2024/05/14 08:09
/** * 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 ret; void DFS(TreeNode *root, int remain) { if (!root) return; if (!root->left && !root->right && root->val == remain) { ret = true; return; } DFS(root->left, remain - root->val); DFS(root->right, remain - root->val); } bool hasPathSum(TreeNode *root, int sum) { // Start typing your C/C++ solution below // DO NOT write int main() function ret = false; DFS(root, sum); return ret; }};
Small Case: 8ms
Large Case: 96ms
Time: O(n)
Space: O(1)
- leetcode Path Sum && Path Sum ||
- 【Leetcode】Path Sum (Sum)
- leetcode:leetcode :Path Sum
- 【Leetcode】Path Sum II (Sum)
- LeetCode: Minimum Path Sum
- LeetCode: Path Sum
- LeetCode: Path Sum II
- LeetCode Minimum Path Sum
- LeetCode Path Sum
- LeetCode Path Sum II
- [Leetcode] Path Sum
- [Leetcode] Path Sum II
- LeetCode : Minimum Path Sum
- [Leetcode] Minimum Path Sum
- LeetCode: Path Sum
- LeetCode: Path Sum II
- [LeetCode] Path Sum
- [LeetCode] Path Sum II
- 学习日记:如何写Makefile(二)——规则篇(上)
- Yii:获取根目录的方法
- 学习日记:如何写Makefile(二)——规则篇(中)
- Linux HugePages 配置 与 Oracle 性能关系说明
- [LeetCode] Flatten Binary Tree to Linked List
- [LeetCode] Path Sum
- [LeetCode] Path Sum II
- [LeetCode] Minimum Depth of Binary Tree - BFS
- [LeetCode] Minimum Depth of Binary Tree - DFS
- [LeetCode] Balanced Binary Tree
- 3DSMax常用快捷键
- [gpu pro]Fast, Stencil-Based Multiresolution Splatting for Indirect Illumination
- 一个非常好的学习SQL的网站 http://ezlearncomputer.com
- 一文讲清楚Windows Mobile和Wince(Windows Embedded CE)的字符集问题