[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 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.
解析
递归解法
正常的树的递归操作。
非递归,使用队列
记录每条路径的值。
代码
递归解法
/**
* 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) {
if (null == root) {
return false;
}
if (root.val == sum && root.left == null && root.right == null) {
return true;
}
boolean leftFlag = hasPathSum(root.left, sum - root.val);
boolean rightFlag = hasPathSum(root.right, sum - root.val);
return leftFlag || rightFlag;
}
}
非递归,使用队列
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null) return false; LinkedList<TreeNode> nodes = new LinkedList<TreeNode>();
LinkedList<Integer> values = new LinkedList<Integer>(); nodes.add(root);
values.add(root.val); while(!nodes.isEmpty()){
TreeNode curr = nodes.poll();
int sumValue = values.poll(); if(curr.left == null && curr.right == null && sumValue==sum){
return true;
} if(curr.left != null){
nodes.add(curr.left);
values.add(sumValue+curr.left.val);
} if(curr.right != null){
nodes.add(curr.right);
values.add(sumValue+curr.right.val);
}
} return false;
}
}
[LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)的更多相关文章
- (二叉树 DFS 递归) 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 二叉树的路径和 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 路径和
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 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- [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(路径和)(BT、DP)(*)
翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...
- 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 ...
- 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)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- 【Java】【泛型】
泛型的优点使⽤泛型有下⾯⼏个优点:1.类型安全2.向后兼容3.层次清晰4.性能较⾼,⽤GJ(泛型JAVA)编写的代码可以为java编译器和虚拟机带来更多的类型信息,这些信息对java程序做进⼀步优化提 ...
- ArrayList的详解
数组一旦给定大小就是固定的,只能放同类型的不能再改,还有一种高级的可扩充的,就是arrayList类,被称作动态数组或者集合. 使用步骤: 1. 引用命名空间system.collections: 2 ...
- gc调优我们到底在调整什么
java开发一般都会涉及到jvm调优其中gc调优是个重点项.那gc调优调整的究竟是什么呢准确来说是业务.下面围绕这个话题展开 起因 为什么说是业务呢得从cc++开始说起如果说是用c/c++做开发运行的 ...
- leecode第十四题(最长公共前缀)
class Solution { public: string longestCommonPrefix(vector<string>& strs) { string res=&qu ...
- 对象反序列化出现类型不匹配的情况(spring-boot-devtools)
目前在做springboot项目的shiro session redis共享功能.但是有一个对象我把它放到redis中之后再取出来就会出现类型不匹配的异常 AuthorizationUser user ...
- Fiddler 简单介绍
fiddler 也已经使用了几年了,前面做免登录时就是用了fiddler,为了抓取cookie等信息.但是一直没有对他进行整理出一篇文章来介绍其使用. Fiddler的基本介绍 Fiddler的官方网 ...
- 分享:selenium(一) xpath
xpath无所不能定位. https://www.w3.org/TR/xpath/all/#axes 两个神器:firebug.xpath-checker 举例:混合定位 //td[a//fron ...
- Ubuntu 16.04 构建 Headless VNC 服务器
终于放弃 Vino 了, 稳定性太低了. 而且,拔了显示器之后,总出现分辨率不对的问题. 于是,构建了一个 xfce4 + tightvnc 的 解决方案. 1) 把Vino相关的自启动都关了. (v ...
- R语言中知识点总结(一)
source("http://bioconductor.org/biocLite.R") biocLite("GEOquery") library(Biobas ...
- 当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出?
当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出? 问题: orderStatus 和 payStatus都是枚举类,并且枚举的个数达地10来个,我们不可能在模板页面(jsp/ftl ...