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 ...
随机推荐
- require和require.async的区别
本文用seajs来讲解两种模块加载方式require和require.async的区别,类似java里的import,php里的include. <!DOCTYPE html> <h ...
- mybatis学习笔记(三)-- 优化数据库连接配置
原来直接把数据库连接配置信息写在conf.xml配置中,如下 <?xml version="1.0" encoding="UTF-8"?> < ...
- mySql---剖析InnoDB索引原理
摘要: 本篇为参考别人的文章(http://blog.csdn.net/voidccc/article/details/40077329) 1 各种树形结构 本来不打算从二叉搜索树开始,因为网上已经有 ...
- Hadoop化繁为简(一)-从安装Linux到搭建集群环境
简介与环境准备 hadoop的核心是分布式文件系统HDFS以及批处理计算MapReduce.近年,随着大数据.云计算.物联网的兴起,也极大的吸引了我的兴趣,看了网上很多文章,感觉还是云里雾里,很多不必 ...
- cocos2d-x学习资源汇总
http://blog.csdn.net/akof1314 http://blog.csdn.net/bill_man/ http://blog.csdn.net/fylz1125/ MoonWa ...
- hdu 4643 GSM 计算几何 - 点线关系
/* hdu 4643 GSM 计算几何 - 点线关系 N个城市,任意两个城市之间都有沿他们之间直线的铁路 M个基站 问从城市A到城市B需要切换几次基站 当从基站a切换到基站b时,切换的地点就是ab的 ...
- 使用jQuery实现input数值的增量和减量
在很多电商网站中,在购物车所在页面,涉及到商品数量的时候,都会提供一个+号按钮和-号按钮来实现增1和减1,并且只允许input中输入数值.Bootstrap TouchSpin这款插件就是针对此需求而 ...
- Delphi 包的设计思想及它与PAS、BPL、DCU、DLL、OXC的关系
一.Delphi中各种文件的介绍,及其关系. OXC: ActiveX控件.会被安装到某一个组件包DPK中,在Imports中会创建PAS.DCU.DCR三个文件. DLL: 动态链接库文件,它的Ex ...
- 得到WAV文件的长度
long getVideoLength (char *fileName){ MCI_OPEN_PARMS mciOpenParms; MCI_STATUS_PARMS mciStatusPar ...
- 【Devops】【docker】【CI/CD】3.Jenkins+GitLab+docker+springboot 实现自动化部署
==================================================================================================== ...