path-sum
来源:互联网 发布:mysql导入sql文件命令 编辑:程序博客网 时间:2024/06/06 17:24
题目:
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 andsum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path5->4->11->2which sum is 22.
程序:
class Solution {public: bool hasPathSum(TreeNode *root, int sum) { if(root==NULL) return false; bool flag=false; solve(flag,root,sum); return flag; } void solve(bool &flag, TreeNode *root,int sum) { if(root&&root->left==NULL&&root->right==NULL&&sum==root->val) { flag=true; return; } if(root==NULL) return; solve(flag,root->left,sum-root->val); solve(flag,root->right,sum-root->val); }};
阅读全文
0 0
- Path Sum && Path Sum ||
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- create-react-app按需加载以及部署
- Java进阶(七)static关键字
- hibernate注解整合spring
- pandas基础__之__对数据进行筛选和排序
- 范围for循环(c++ primer 笔记)
- path-sum
- springboot结合redis实现redis订阅发布模式
- iOS---scrollview自动下移问题
- 浅谈PHP与手机APP开发即API接口开发
- new与malloc
- leetcode 274. H-Index
- 编辑修改公众号资料-微信公众号使用教程6
- EditText属性大全详解
- JAVA中Action层, Service层 ,modle层 和 Dao层的功能区分