Leetcode 112, Path Sum
来源:互联网 发布:linux zip命令 密码 编辑:程序博客网 时间:2024/06/05 11:53
Leetcode 112, Path Sum, 难度easy
这周上的是DFS,所以就先做了这道DFS的题,解题思路就是深搜一遍,到达一个结点时先判断左右子节点是否为空,都为空说明是叶子节点,然后判断当前的和是否等于sum返回true or false,非叶子节点则进行下一步的深搜。
以下是代码:
class Solution {public: bool hasPathSum(TreeNode* root, int sum) { int res = 0; if(root == NULL) return false; else return binaryTree(root, sum, res); } bool binaryTree(TreeNode* root, int sum, int res) { res += root -> val; bool left = root -> left == NULL? (root -> right == NULL? sum == res: false) : binaryTree(root -> left, sum, res); bool right = root -> right == NULL? (root -> left == NULL? sum == res: false) : binaryTree(root -> right, sum, res); if(left || right) return true; return false; }};
阅读全文
0 0
- LeetCode(112)Path Sum
- LeetCode 112 Path Sum
- LeetCode: Path Sum [112]
- LeetCode 112 Path Sum
- [leetcode 112] Path Sum
- leetCode #112 path sum
- [LeetCode 112]Path Sum
- leetcode || 112、Path Sum
- LeetCode 112: Path Sum
- LeetCode 112 Path Sum
- leetcode[112]:Path Sum
- leetcode-112-Path Sum
- Leetcode# 112 Path Sum
- leetcode 112: Path Sum
- LeetCode 112: Path Sum
- [leetcode 112]Path Sum
- LeetCode 112 Path Sum
- LeetCode 112:Path Sum
- eval('('+***+')')对json数据格式的作用
- Windows7 64 位系统安装Oracle VM VirtualBox 后没有Ubuntu/Linux 64-bit选项
- Php中的静态变量和静态方法
- redis Java客户jedis的使用。
- js判断微信内核浏览器
- Leetcode 112, Path Sum
- Ubuntu字符界面输入密码始终提示错误 login incorrect 解决办法
- 高效编程:Redis实战与源码剖析(一)
- LinuxRabbitMQ的安装
- 关于友盟s=-11;s1=accs bindapp error!的解决处理
- QT4.8.5+qt-vs-addin-1.1.11+VS2010安装配置和QT工程的新建和加载
- 写博客的一点感想--记录下我的技术经验足迹
- starUML下载、安装、破解全过程
- POJ