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. 【wikioi】1285 宠物收养所

    题目链接:http://www.wikioi.com/problem/1285/ 算法:Splay 刚开始看到这题,就注意到特征abs了,并且数据n<=80000显然不能暴力,只能用nlgn的做 ...

  2. COJ990 WZJ的数据结构(负十)

    DFS序(带入栈出栈标记): 对于一个节点,我们用L[i]和R[i]表示它入栈和出栈的时间.这样[L[i],R[i]]就表示了以i为根的区间. 我们还要将入栈的符号为+,出栈的符号为-,那么令V[i] ...

  3. 安装win7 ubuntu双系统

    http://www.cnblogs.com/shengansong/archive/2011/10/23/2221716.html http://www.cnblogs.com/shenganson ...

  4. JAVA_RSA密钥生成

    在网上找了下RSA的密钥的创建,结果全是用java序列化PublicKey和PrivateKey来保存,就自己写了个RSA公钥和私钥的创建,及进行Base64编码后保存. 这里用到了 bcprov-j ...

  5. iOS开发之UITextField的使用详解

    UITextField的使用详解 UITextField控件是开发中,使用频率比较高的控件了,那么有必要总结一下. 一.UITextField手动编写控件 UITextField  *txtAccou ...

  6. C Memory Layout C语言中的内存布局

    在C语言中,内存的主要分为下列几部分: 1. Text/Code Segment 文本/代码区 2. Initialized Data Segments 初始化的数据区 3. Uninitialize ...

  7. css样式表:样式分类,选择器。样式属性,格式与布局

    样式表分类: 1.内联样式表, 和html联合显示,例:<p style="font-size:14px;">内联样式表</p> 2.内嵌样式表 作为一个独 ...

  8. MIB-II

    . 1.3.6.1.2.1

  9. Linux环境下实现生产者消费者问题

    #include <stdio.h> #include <semaphore.h> #include <stdlib.h> #include <pthread ...

  10. poj2387 初涉最短路

    前两天自学了一点点最短路..看起来很简单的样子... 就去kuangbin的专题找了最简单的一道题练手..然后被自己萌萌的三重for循环超时虐的不要不要的~ 松弛虽然会但是用的十分之不熟练... 代码 ...