LeetCode Path Sum
来源:互联网 发布:微信牛牛机器人软件 编辑:程序博客网 时间:2024/05/20 15:41
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.
/* * bts.cpp * * Created on: 2015年4月14日 * Author: judyge */#include <stdio.h>#include <iostream>#include <stack>#include <map>using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} };// 因为是判断目的是否可达 深度优先搜索好 用递归bool dfs(TreeNode *node, int sum, int curSum){if (node == NULL) return false; //空树if (node->left == NULL && node->right == NULL) return curSum + node->val == sum; //只有根节点的树return dfs(node->left, sum, curSum + node->val) || dfs(node->right, sum, curSum + node->val);//递归遍历左右两棵树数}bool hasPathSum(TreeNode *root, int sum) { return dfs(root, sum, 0);}
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
- First Blog
- 【ThinkingInJava】6、测试自己的输出类库
- Ubuntu下桌面死机的解决方法,无须重启
- 图示RFID通信过程
- Mysql创建数据库及表的方法-可视化软件Navicat
- LeetCode Path Sum
- REST for Everyone
- IOS新浪微博开发--分享、授权登录
- 基础类型和包装类的区别及作用
- 彻底解决post各种乱码问题
- Eclipse编码
- iOS Voip 后台运行的原理
- 【WEB】struts2整合spring原理以及源码剖析
- 添加本地仓库到远程仓库