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 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.
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
字符串中间还要加符号,头一次见
[奇葩corner case]:
两端都是空,只有root一个点也是能求和的,第一次见。下次二叉树求和的时候只要有点就能加
[思维问题]:
不知道怎么利用sum:变成参数即可
[一句话思路]:
还是没有汇总的traverse,把sum参与进来,把sum参数化变成一个参数就行了
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
path sum后续系列
[代码风格] :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
//root is null
if (root == null) {
return false;
}
//only root is not null
if (root.left == null && root.right == null) {
return root.val == sum;
}
//remaning sum in left or sum in right
return (hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val));
}
}
112. Path Sum二叉树路径和的更多相关文章
- [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 二叉树的路径和 C++
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(路径和)(BT、DP)(*)
翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...
- Leetcoede 112 Path Sum 二叉树
二叉树的从叶子到根的和是否存在 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * ...
- LeetCode 112. Path Sum(路径和是否可为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] 437. Path Sum III 路径和 III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
随机推荐
- vim 编辑技巧
vi是linux下最常用的编辑器,vim是vi的加强版,本篇将介绍vim的一些快捷键和使用技巧,借鉴网上其他文章表示
- linux 定时任务 Crond Crontab
定时任务http://www.cnblogs.com/chensiqiqi/p/6367890.html http://www.cnblogs.com/chensiqiqi/p/6389611.htm ...
- linux tcp server demo
#include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <ne ...
- Lock分析
Lock接口是锁的实现,用来控制多个线程访问共享资源的方式,是在java 1.5的时候引入的,在此之前,只能通过synchronized的方式来取得对象的锁. synchronized中的锁是隐式 ...
- 简单的PL/SQl链接远程ORACLE数据库方法
简单的PL/SQl链接远程ORACLE数据库方法 PLSQL Developer新手使用教程 pasting
- json-lib使用笔记
今天再来记录一款生成JSON的工具——json-lib,它比较与我之前使用的FastJSON来说,使用都是很简单的,但是要使用json-lib要需要的jar包可真不少,所需要的依赖jar有八九个之多, ...
- 【UVALive】2678 Subsequence(尺取法)
题目 传送门:QWQ 分析 一开始没看到都是正整数根本不会做...... 看到了就是水题了.(但还是sb WA了一发) 尺取法搞一搞 代码 #include <bits/stdc++.h> ...
- mysql数据安全一之数据恢复案例
mysql数据安全一之数据恢复案例 --chenjianwen 应用场景:适宜开启binlog 日志功能,定时备份并使用--master-data参数备份,在某个时间点丢失数据,用于数据恢复 开篇总结 ...
- node启动appium.js
node启动appium.js,appium.js目录中不能有空格或者(x86)等字样
- IDA Pro 权威指南学习笔记(十) - 栈帧
栈帧(stack frame)是在程序的运行时栈中分配的内存块,用于特定的函数调用 如果一个函数没有执行则不需要内存,当函数被调用时就需要用到内存 1.传给函数的参数的值需要存储到函数能够找到它们的位 ...