题:

  

解:

  这道题考的是如何找出一个二叉树里所有的序列。

  我的思路是先从根节点开始遍历,找出所有的子节点,因为每个子节点只有一个父节点,再根据每个子节点向上遍历找出所有的序列,再判断序列的总和。

  这样解效率不高,但暂时只能想到这样。如果您有其余的解法,期望告知。

代码:

  

 package com.lintcode;

 import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List; import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode; /**
* 二叉树的路径和
* 给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径。
* 一个有效的路径,指的是从根节点到叶节点的路径。
* @author Administrator
*/
public class Test_002 {
// 得到所有最下面的子节点,再根据子节点向上遍历得到序列,每个子节点只能有一个父节点,
// 所以可以得到所有的序列,再判断序列的值是否等于需要的值。
/**
* @param args
*/
public static void main(String[] args) {
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(2);
node1.add(new DefaultMutableTreeNode(2));
DefaultMutableTreeNode node7 = new DefaultMutableTreeNode(3);
node7.add(new DefaultMutableTreeNode(6));
node1.add(node7);
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(4);
DefaultMutableTreeNode top = new DefaultMutableTreeNode(1);
top.add(node1);
top.add(node2);
binaryTreePathSum(top,5);
for (List<Integer> list : result) {
System.out.println(list);
}
}
static List<List<Integer>> result = new ArrayList<List<Integer>>();
/**
* @param root the root of binary tree
* @param target an integer
* @return all valid paths
*/
public static void binaryTreePathSum(TreeNode root, int target) {
if (root.getChildCount()!=0) {
Enumeration nodes = root.children();
while (nodes.hasMoreElements()) {
binaryTreePathSum((TreeNode)nodes.nextElement(),5);
}
} else {
addList(root,new ArrayList<Integer>(), 5);
}
} public static void addList(TreeNode root, List<Integer> temp, int target) {
List<Integer> list = temp;
list.add(Integer.parseInt(root.toString()));
if (root.getParent()!=null) {
addList(root.getParent(), list, 5);
} else {
Collections.sort(list);
int count = 0;
for (Integer integer : list) {
count+=integer;
}
if (count==target) {
result.add(list);
}
}
}
}

  这段代码在我的机子上运行是没问题的,但是在LintCode上面运行时总是提示找不到TreeNode的getChildCount方法,如果您知道原因的话,期望留言,谢谢。

Java实现求二叉树的路径和的更多相关文章

  1. [Leetcode] Binary tree maximum path sum求二叉树最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  2. java 递归求二叉树深度

    给定二叉树,找到它的最大深度. 最大深度是从根节点到最远叶节点的最长路径上的节点数. 注意:叶子是没有子节点的节点. Example: Given binary tree [3,9,20,null,n ...

  3. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  4. [LeetCode] Path Sum IV 二叉树的路径和之四

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  5. 二叉树 Java 实现 前序遍历 中序遍历 后序遍历 层级遍历 获取叶节点 宽度 ,高度,队列实现二叉树遍历 求二叉树的最大距离

    数据结构中一直对二叉树不是很了解,今天趁着这个时间整理一下 许多实际问题抽象出来的数据结构往往是二叉树的形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显 ...

  6. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  7. [LeetCode] 124. Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  8. 求二叉树的深度,从根节点到叶子节点的最大值,以及最大路径(python代码实现)

    首先定义一个节点类,包含三个成员变量,分别是节点值,左指针,右指针,如下代码所示: class Node(object): def __init__(self, value): self.value ...

  9. LeetCode OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

随机推荐

  1. D广搜

    <span style="color:#330099;">/* D - 广搜 基础 Time Limit:1000MS Memory Limit:30000KB 64b ...

  2. 异或巧用:Single Number

    异或巧用:Single Number 今天刷leetcode,碰到了到题Single Number.认为解答非常巧妙,故记之... 题目: Given an array of integers, ev ...

  3. 浅谈c#的三个高级参数ref out 和Params C#中is与as的区别分析 “登陆”与“登录”有何区别 经典SQL语句大全(绝对的经典)

    浅谈c#的三个高级参数ref out 和Params   c#的三个高级参数ref out 和Params 前言:在我们学习c#基础的时候,我们会学习到c#的三个高级的参数,分别是out .ref 和 ...

  4. NoSQL之Redis探析

    下载地址:wget http://download.redis.io/releases/redis-2.8.8.tar.gz安装steps:1 下载Official Website : http:// ...

  5. VC2010 利用 def 文件生成 dll 文件的方法

    近期有个需求,要生成一个dll 文件.文件里的函数都是採用 stdcall 函数调用约定,可是不希望函数名被修饰(add 被修饰成 add@8). 这时就要用def 文件了. 比方我有以下两个函数: ...

  6. Hibernate 之 Persistence

    分享自:  http://blog.csdn.net/jnqqls/article/details/8276059 在我们之前的文章已经了解到,Hibernate的汉语解释叫做冬眠,而这个冬眠我个人理 ...

  7. SSH三大框架整合配置详细步骤(1)

    配置Struts2.0 3.1 基础配置 1)引入Struts必需的五个jar包.下载struts-2.1.6-all.zip解压后,struts-2.1.6\lib目录下是struts所有的相关ja ...

  8. Koa2学习(八)使用session

    Koa2学习(八)使用session koa2框架不提供session的处理方法,这里我们需要借助一个第三方中间件koa-session来处理session. 先安装插件: $ npm i koa-s ...

  9. (21) java web的struts2框架的使用-Action实现的三种方式

    上一篇介绍了struts使用的四个步骤. 其中在开发action的时候,可以有三种实现方式: 1,写一个类,继承与ActionSupport 2,写一个类,实现Action接口 3,写一个类,实现业务 ...

  10. 设计模式-(13)访问者模式 (swift版)

    一,概念 访问者模式,是行为型设计模式之一.访问者模式是一种将数据操作与数据结构分离的设计模式,它可以算是 23 中设计模式中最复杂的一个,但它的使用频率并不是很高,大多数情况下,你并不需要使用访问者 ...