LeetCode : Path Sum
来源:互联网 发布:网络机房建设 编辑:程序博客网 时间:2024/05/29 13:16
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 a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool hasPathSum(TreeNode* root, int sum) { if(!root) return false; if(root->left==NULL&&root->right==NULL) return root->val==sum; else { return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val); } }};
0 0
- 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
- 区分java字节(Byte)流和字符(character)流
- 用训练好的模型(caffemodel)来分类新的图片
- 数学建模——层次分析法
- 2017.4.9
- Linux高级管理之管理系统中的简单分区和文件系统
- LeetCode : Path Sum
- 【Dubbo初识——因为遇见你】
- BackTracking回溯
- “自我炒作”雄安新区 遭监管层严打
- Android Context完全解析,你所不知道的Context的各种细节
- C语言中strand() rand() time()函数的简单介绍及获取随机数的方法
- 【LeetCode】Group Anagrams 解题报告
- 解决分类样本不平衡问题
- 停车场管理系统(数据库)