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 andsum = 22,

              5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1

return true, as there exist a root-to-leaf path5->4->11->2which sum is 22.

代码 1

import java.util.ArrayList;

class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution {
ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> arr=new ArrayList<Integer>();
public boolean hasPathSum(TreeNode root, int sum) {
if(root==null)return false;
isPath(root,0,sum);
if(result.isEmpty())return false;
else return true; }
private void isPath(TreeNode root, int sum, int target) {
if(root==null)return;
else{
sum+=root.val;
arr.add(root.val);
if(root.left==null&&root.right==null&&sum==target){
result.add(new ArrayList<Integer>(arr));
}
isPath(root.left, sum, target);
isPath(root.right, sum, target);
arr.remove(arr.size()-1);
sum-=root.val;
} }
} 代码二:
import java.util.ArrayList;

class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution { public boolean hasPathSum(TreeNode root, int sum) {
return hasPathSumHelper(root, sum);
} private boolean hasPathSumHelper(TreeNode root, int sum) {
// TODO Auto-generated method stub
if(root==null)return false;
if(root.left==null&&root.right==null&&sum==root.val)return true;
return hasPathSumHelper(root.left, sum-root.val)||hasPathSumHelper(root.right, sum-root.val);
}
}

 

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 andsum =的更多相关文章

  1. 1022. Sum of Root To Leaf Binary Numbers从根到叶的二进制数之和

    网址:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ 递归调用求和,同时注意%1000000007的位置 /** * ...

  2. 129. Sum Root to Leaf Numbers(Tree; DFS)

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  3. LeetCode :: Sum Root to Leaf Numbers [tree、dfs]

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  4. LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)

    1022. 从根到叶的二进制数之和 1022. Sum of Root To Leaf Binary Numbers 题目描述 Given a binary tree, each node has v ...

  5. 【Leetcode_easy】1022. Sum of Root To Leaf Binary Numbers

    problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binar ...

  6. [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  7. 【leetcode】Sum Root to Leaf Numbers(hard)

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  8. 129. Sum Root to Leaf Numbers

    题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...

  9. Leetcode Sum Root to Leaf Numbers

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

随机推荐

  1. Sublime Text 2配置

    gedit用了很久,终于换编辑器了T_T Sublime Text 自行百度谷歌. 一开始我在官网下载的压缩包,然后自己配置.搞了半天后果断删掉...还是用源的自动安装吧.T_T 恩.下面的命令 su ...

  2. opengl中拾取操作的实现

    opengl采用一种比较复杂的方式来实现拾取操作,即选择模式.选择模式是一种绘制模式,它的基本思想是在一次拾取操作时,系统会根据拾取操作的参数(如鼠标位置)生成一个特定视景体,然后又系统重新绘制场景中 ...

  3. 获取Dell,Lenovo电脑的保修期

    2015-4-6写的代码(Dell), 不知道如何对报错进行友好化处理,于是采用了"非空"和"非空的补集"处理方式. $service = New-WebSer ...

  4. 2016.07.14,英语,《Vocabulary Builder》Unit 25

    verb: comes from the Latin verbum, meaning 'word'. verbally: ['vɜːbəli] adv. 口头地,词句地, 逐字地 verbalize: ...

  5. empty()与remove([expr])的区别.转

    jquery之empty()与remove()区别   要用到移除指定元素的时候,发现empty()与remove([expr])都可以用来实现.可仔细观察效果的话就可以发现.empty()是只移除了 ...

  6. DropzoneJS 使用指南

    原文链接http://segmentfault.com/a/1190000004045240 官方文档: http://www.dropzonejs.com/ Github: https://gith ...

  7. JsonCpp 简单使用

    [转]自: http://www.cnblogs.com/ytjjyy/archive/2012/04/17/2453348.html JsonCpp 是一个C++用来处理JSON 数据的开发包.下面 ...

  8. ServletContextDemo

    1.servlet 之间共享数据 package xw.servlet; import javax.servlet.ServletContext; import javax.servlet.http. ...

  9. (转)freemakeer初入门

    在web开发过程中,尤其是后台管理系统的开发中,少不了增删改成的基础操作,原来我自己的做法是一份一份的拷贝粘贴,然后修改其中的不同,然而这样既枯燥无味又浪费了大量的时间,所以根据自己项目结构的特点写了 ...

  10. 后台获取前台runat=server的select的值

    <li> <asp:Label ID="Lpro" runat="server" Text="省份:" CssClass= ...