[刷题] 112 Path Sum
要求
- 给出一个二叉树及数字sum,判断是否存在一条从根到叶子的路径,路径上的所有节点和为sum
实现
- 转化为寻找左右子树上和为 sum-root 的路径,到达叶子节点时递归终止
- 注意只有一个孩子时,根节点本身不构成一条路径,如下图sum=5的情况,终止条件是不对的
1 class Solution {
2 public:
3 bool hasPathSum(TreeNode* root, int sum) {
4
5 if( root == NULL )
6 return false;
7
8 if( root->left == NULL && root->right == NULL )
9 return root->val == sum;
10
11 if( hasPathSum( root->left , sum - root->val ) )
12 return true;
13
14 if( hasPathSum( root->right , sum - root->val ) )
15 return true;
16
17 return false;
18 }
19 };

相关
- 111 Minimum Depth of Binary Tree
- 404 Sum of Left Leaves
[刷题] 112 Path Sum的更多相关文章
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- 112. Path Sum二叉树路径和
[抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- [LeetCode] 112. Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- LeetCode OJ 112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Leetcode 112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode]题解(python):112 Path Sum
题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...
随机推荐
- PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642
PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...
- Redis生产环境节点宕机问题报错及恢复排错
Redis故障发现 主观下线 当cluster-node-timeout时间内某节点无法与另一个节点顺利完成ping消息通信时,则将该节点标记为主观下线状态. 客观下线 当某个节点判断另一个节点主观下 ...
- [Fundamental of Power Electronics]-PART II-7. 交流等效电路建模-7.2 基本交流建模方法
7.2 基本交流建模方法 在本节中,PWM变换器的交流小信号模型导出步骤将被推导和解释.关键步骤是:(a)利用小纹波近似的动态版本,建立了与电感和电容波形的低频平均值相关的方程式,(b)平均方程的扰动 ...
- OOJML系列总结
目录 0x0 JML理论相关 0.0 概念及作用 0.1 JML语法学习 0x1 使用openJml以及JMLUnitNG 1.0 使用openjml 1.1使用JMLUnitNG 0x2 作业架构设 ...
- (一)Struts2框架概述
一.struts2发展历史 经过很多年发展,Struts1已经成为了高度成熟的框架,但随着时间的发展,Struts1的局限性和缺点不断的暴露出来. 现在Struts已经分化成了两个框架 ...
- No_leak(ret2ROP + 低字节改写到syscall)
No_leak 有这种题,题目很短小,只有一个read函数,没有输出函数,这样的题怎么解呢?当然首先想到的是ret2dl,但是那个有点儿复杂.下面我来介绍一种简单的解法. 代码如下: //gcc 1. ...
- Jmeter(四十三) - 从入门到精通高级篇 - Jmeter之IP伪装和欺骗(详解教程)
1.简介 我们从小接受的教育就是不要撒谎,要做一个诚实的孩子,但是在现实生活中有时候说一个善意的谎言也不是可以的.这里由于服务器各种安全机制的限制和校验,因此我们不得不欺骗一下服务器,今天宏哥就给大家 ...
- Tomcat配置及网站创建教程(IDEA)
Tomcat在本机的配置 解压 解压Tomcat压缩包后就算安装完成,解压完成生成文件夹 配置环境变量 1.配置JAVA_HOME 控制面板--系统--查看高级系统设置--环境变量--系统环境变量 新 ...
- 记一次linux下安装ftp的愉快体验
三三两两,试了几次就出来了,挺开心的 linux安装vsftpd,请自行百度 贴出部分配置点 阿里云服务器,开发相关端口以及部分区域端口访问 /etc/pad.d/vsftpd添加部分注释 #%PAM ...
- Linux下查看CPU、内存占用率
linux下查看最消耗CPU.内存的进程 CPU占用最多的前10个进程: ps auxw|head -1;ps auxw|sort -rn -k3|head -10 内存消耗最多的前10个进程: ps ...