Java实现求二叉树的路径和
题:

解:
这道题考的是如何找出一个二叉树里所有的序列。
我的思路是先从根节点开始遍历,找出所有的子节点,因为每个子节点只有一个父节点,再根据每个子节点向上遍历找出所有的序列,再判断序列的总和。
这样解效率不高,但暂时只能想到这样。如果您有其余的解法,期望告知。
代码:
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实现求二叉树的路径和的更多相关文章
- [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. ...
- java 递归求二叉树深度
给定二叉树,找到它的最大深度. 最大深度是从根节点到最远叶节点的最长路径上的节点数. 注意:叶子是没有子节点的节点. Example: Given binary tree [3,9,20,null,n ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [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 ...
- 二叉树 Java 实现 前序遍历 中序遍历 后序遍历 层级遍历 获取叶节点 宽度 ,高度,队列实现二叉树遍历 求二叉树的最大距离
数据结构中一直对二叉树不是很了解,今天趁着这个时间整理一下 许多实际问题抽象出来的数据结构往往是二叉树的形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显 ...
- [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. ...
- [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 ...
- 求二叉树的深度,从根节点到叶子节点的最大值,以及最大路径(python代码实现)
首先定义一个节点类,包含三个成员变量,分别是节点值,左指针,右指针,如下代码所示: class Node(object): def __init__(self, value): self.value ...
- 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 ...
随机推荐
- nginx内存池
一.设计原则 (1)降低内存碎片 (2)降低向操作系统申请内存的次数 (3)减少各个模块的开发效率 二.源代码结构 struct ngx_pool_s { ngx_pool_data_t ...
- ZOJ 2859 二维线段树
思路:自己写的第二发二维线段树1A.哈哈,看来对二维的push操作比較了解了:可是还没遇到在两个线段树中同一时候进行push操作的,事实上这题我是想在x维和y维同一时候进行push操作的.可是想了好久 ...
- Asp.net MVC 简单分页 自做简单分页
Asp.net MVC 简单分页: public static string Pager(int page,int pageSize,int total) { ...
- EditText设置光标位置问题
普通设置 EditText 光标显示位置的方法就是 et.setSelection(text.length()); et.setSelection(0); 设置0 就是第一位了. 设置text长度就最 ...
- STM32通过调用库函数进行编程
1.调用库函数编程和直接配置寄存器编程的差别: 2.CMSIS标准: 3.STM32库函数的组织: 4.程序例举: 调用库函数实现通过USART发送数据(26个大写的英文字母) 首先:在主函数部分先要 ...
- linux 【目录】
[第一篇]linux[目录] [第五篇]特殊权限及定时任务 [第六篇]用户和用户管理及定时任务复习
- UVA 437 The Tower of Babylon巴比伦塔
题意:有n(n≤30)种立方体,每种有无穷多个.要求选一些立方体摞成一根尽量高的柱子(可以自行选择哪一条边作为高),使得每个立方体的底面长宽分别严格小于它下方立方体的底面长宽. 评测地址:http:/ ...
- PS人物脸部去高光简单之法
案例素材图: 方法原理步骤:得到高光面的选区,然后吸取高光面附近的颜色填充上去,这样就达到了去高光的效果. 得到高光选区的方法有很多种,要提取这种选区,通过阿尔法通道是最合适不过的了,本案例就通过阿尔 ...
- 《31天成为IT服务达人》--做事篇(第四章)之如何找目标
前面介绍了什么是IT服务.以下几章将介绍IT服务该怎么做.在聊怎么做之前.想起几句流行的告白和准备入行IT服务事业的朋友共勉. 当你的才华 还撑不起你的野心时 就应该静下心来 学习 --- 当你 ...
- YTU 2952: A代码填充--谁挡住了我
2952: A代码填充--谁挡住了我 时间限制: 1 Sec 内存限制: 128 MB 提交: 135 解决: 38 题目描述 n个人前后站成一列,对于队列中的任意一个人,如果排在他前面的人的身高 ...