Leetcode:Path Sum 菜鸟解法
来源:互联网 发布:linux 复制目录及文件 编辑:程序博客网 时间:2024/06/05 14:45
相对上题来说这题就简单多了。
对于根节点来说,如果叶到根要满足和为sum,则必须左或右子树满足和为sum-root->val
当左右均为null时,即为叶子节点。
代码如下:
class Solution {public: bool hasPathSum(TreeNode *root, int sum) { if(root ==NULL) return false; if(root->left==NULL&&root->right==NULL&&root->val==sum) return true; return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val); }};
0 0
- Leetcode:Path Sum 菜鸟解法
- Leetcode:Binary Tree Maximum Path Sum菜鸟解法
- leetcode:two sum 菜鸟解法
- leetcode [Path Sum]//待整理多种解法
- leetcode: Sum Root to Leaf Numbers 菜鸟解法
- leetcode:path sum(I) 递归与非递归解法
- 【LeetCode】112. Path Sum Java,C++,解法及注释
- LeetCode path Sum i ,ii递归和迭代解法
- leetcode[Path Sum III]//待整理多种解法
- leetcode Path Sum && Path Sum ||
- 【Leetcode】Path Sum (Sum)
- Path Sum问题及解法
- leetcode:leetcode :Path Sum
- leetcode:add binary 菜鸟解法
- leetcode: Reverse Integer 菜鸟解法
- leetcode:Maximum Subarray 菜鸟解法
- leetcode:Single Number 菜鸟解法
- leetcode:Sqrt(x) 菜鸟解法
- LPC2138编程时出现错误“Timer.sct(7): error: L6236E: No section matches selector - no section to be FIRST/LAS
- java软件复杂运算,同时显示进度条的一种方法
- 一款桌面英语翻译软件介绍
- Oracle SQL*Loader
- Linux的ACL权限
- Leetcode:Path Sum 菜鸟解法
- hdoj 1874 通畅工程续 (最短路)
- Java中Runnable和Thread的区别
- 00-自测1. 打印沙漏(20)
- 阿里云cos,memcached无法使用
- oracle使用pagesize命令
- 黑马程序员--对File类的理解
- 记录一些链接,方便自己有空的时候慢慢查看
- 64位Win8安装Oracle12C临时位置权限错误解决方案