题:

  

解:

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

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

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

代码:

  

 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. Linux下获取线程TID的方法

    如何获取进程的PID(process ID)? 可以使用: #include <unistd.h> pid_t getpid(void); 通过查看头文件说明,可以得到更详细的信息: fi ...

  2. CSS 相对|绝对(relative/absolute)定位系列(一)

    一.有话要说 以前写内容基本上都是:眼睛一亮——哟呵,这个不错,写!然后去古人所说的茅房里蹲会儿,就有写作的思路了.但是,构思相对/绝对(relative/absolute)定位系列却有好些时日,考虑 ...

  3. BZOJ 1122 POI2008 账本BBB 单调队列

    题目大意:给定一个由+1和−1构成的长度为n的序列,提供两种操作: 1.将某一位取反,花销为x 2.将最后一位移动到前一位.花销为y 要求终于p+sumn=q.且p+sumi≥0(1≤i≤n),求最小 ...

  4. Centos 6.4 实际工作环境搭建(LNMP)

    基本配置 服务器IP设置.编辑网卡配置文件,命令: vi /etc/sysconfig/network-scripts/ifcfg-eth0 注:ifcfg-eth0参数  TYPE=Ethernet ...

  5. python各进制、字节串间的转换

    >>> i = 13 >>> bin(i) '0b1101' >>> oct(i) '0o15' >>> hex(i) '0xd ...

  6. IO获取指定目录及其目录下子目录

    一.需求:获取指定目录下,指定扩展名的文件(包含子目录中的) 二.需要用到的方法 1.FilenameFilter :返回抽象路径名的定义中表示此抽象路径名的目录中的文件的数组.  filter.ac ...

  7. 嵌入式开发之davinci--- 8148/8168/8127 中的图像缩放sclr、swms之后出现图像视频卡顿、屏幕跳跃的问题

    ()问题原因 这边的case链路是这样的camera->sclr(yuv420sp cif)->dup->ipcframeoutm3<->ipcframerocess&l ...

  8. 亿部书城李柯毅:Testin云測可大幅提升产品质量 值得推荐!

    亿部书城李柯毅:Testin云測可大幅提升产品质量 值得推荐! 2014/10/13 · Testin · 开发人员訪谈 成立于2010年的亿部书城.其主营业务为移动增值业务及数字出版业务,由中央部委 ...

  9. 2016/04/13 ①html 中各种分割线------------------------------------------ ② 控制文字显示

    ①各种分割线Html代码 1.<HR> 2.<HR align=center width=300 color=#987cb9 SIZE=1>align 线条位置(可选left. ...

  10. C# ref和out总结

    C# 中ref 与 out 总结   参数的传递一般分为两种:一种是“值传递”即:传递实参的拷贝,既然是拷贝那么在函数中对这个形参所作的任何动作都不会反映到原来的实参中.另外一种是“引用传递”即:传递 ...