Path Sum leetcode java
题目:
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.
题解:
还是对树的操作,递归的解法:
1 public boolean hasPathSum(TreeNode root, int sum) {
2 if(root == null)
3 return false;
4
5 sum -= root.val;
6 if(root.left == null && root.right==null)
7 return sum == 0;
8 else
9 return hasPathSum(root.left,sum) || hasPathSum(root.right,sum);
}
非递归的解法(Reference:http://www.programcreek.com/2013/01/leetcode-path-sum/):
1 public boolean hasPathSum(TreeNode root, int sum) {
2 if(root == null) return false;
3
4 LinkedList<TreeNode> nodes = new LinkedList<TreeNode>();
5 LinkedList<Integer> values = new LinkedList<Integer>();
6
7 nodes.add(root);
8 values.add(root.val);
9
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;
}
Path Sum leetcode java的更多相关文章
- 【LeetCode】Path Sum ---------LeetCode java 小结
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- Binary Tree Maximum Path Sum leetcode java
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- Minimum Path Sum leetcode java
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- 【LeetCode】Path Sum 2 --java 二叉数 深度遍历,保存路径
在Path SUm 1中(http://www.cnblogs.com/hitkb/p/4242822.html) 我们采用栈的形式保存路径,每当找到符合的叶子节点,就将栈内元素输出.注意存在多条路径 ...
- Binary Tree Maximum Path Sum - LeetCode
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- LeetCode算法题-Path Sum(Java实现)
这是悦乐书的第169次更新,第171篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第28题(顺位题号是112).给定二叉树和整数sum,确定树是否具有根到叶路径,使得沿路 ...
- leetcode 113 Path Sum II ----- java
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- Minimum Path Sum [LeetCode]
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Path Sum [LeetCode]
Problem Description: http://oj.leetcode.com/problems/path-sum/ Pretty easy. /** * Definition for bin ...
随机推荐
- Outdated Kotlin Runtime
你的 kotlin 运行时版本 在 1.1.2库中 是 1.1.2 然而插件版本是 1.1.4 . 运行时库 应该被更新,避免兼容问题. Outdated Kotlin Runtime Your ve ...
- BZOJ 3812主旋律
求一个图中强联通图的个数. 一看就是容斥啦,但这种二进制高端操作还是学习一下Candy?dalao 注释在代码里 好久没更了... #include<bits/stdc++.h> usin ...
- 8.4 正睿暑期集训营 Day1
目录 2018.8.4 正睿暑期集训营 Day1 A 数对子 B 逆序对 C 盖房子 考试代码 A B C 2018.8.4 正睿暑期集训营 Day1 时间:4.5h(实际) 期望得分:30+50+3 ...
- 【bfs】BZOJ1102- [POI2007]山峰和山谷Grz
最后刷个水,睡觉去.Bless All! [题目大意] 给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是 ...
- [USACO3.2]Sweet Butter
题目大意: 给定一张$k$个结点,$m$条边的无向图,其中有$n$个点被标记,在这$k$个点中找出一个点使得这个点到那$n$个点的最短距离之和最小,求出这个距离和. 思路: 对于每个标记结点跑最短路, ...
- C# 实现IDisposable的模式
来自MSDN官方文档:http://msdn.microsoft.com/en-us/library/system.configuration.provider.providercollection. ...
- 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作
马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...
- jquery json 格式教程
介绍 我们知道AJAX技术能够使得每一次请求更加迅捷,对于每一次请求返回的不是整个页面,也仅仅是所需要返回的数据.通常AJAX通过返回XML格式的数据,然后再通过客户端复杂的JavaScript脚本解 ...
- CentOS 6.8 安装最新版 Git
CentOS 6.8 自带的 Git 版本为 1.7.1,比较旧,yum 安装也停留在 1.7.1,还是源码编译安装吧. 1. 下载源码: wget -c https://github.com/git ...
- .NET 开源Protobuf-net从入门到精通
<.NET 开源Protobuf-net从入门到精通>课程包含以下两个部分: 一..NET 开源Protobuf-net组件[数据存储篇] 本次分享课程包含以下干货知识点: 1.什么是Pr ...