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 ...
随机推荐
- XxPay支付系统-boot版本 使用
https://segmentfault.com/a/1190000016987391?utm_source=tag-newest 有三个版本: spring boot 版本: spring clou ...
- 输入年份,然后打印出该年的万年历,以及标识出当天日期。相似于linux下的cal -y结果。
public class Permanent { public static boolean isLeapYear(int year){//能被4整除但不能被100整除.或者能被400整除 boole ...
- 【git体验】git原理及基础
原理:分布式版本号控制系统像 Git,Mercurial,Bazaar 以及 Darcs 等,client并不仅仅提取最新版本号 的文件快照,而是把原始的代码仓库完整地镜像下来. 这么一来.不论什么一 ...
- python爬虫爬取内容中,-xa0,-u3000的含义
python爬虫爬取内容中,-xa0,-u3000的含义 - CSDN博客 https://blog.csdn.net/aiwuzhi12/article/details/54866310
- vs2010 创建和发布 webservice
1 打开VS2010,菜单 文件->新建->项目 2 选择[ASP.net 空web应用程序],将其命名为自己想的工程名称. 3 右键点击工程,添加->新建项 选择 web服务 ...
- scikit-learn(1) 第一个例子说明
第一个 scikit-learn例子 ................................................................................. ...
- RK3288 make otapackage 出错的问题【转】
本文转载自:http://blog.csdn.net/u010439962/article/details/51734631 Installed file list: out/target/produ ...
- UVA11183 Teen Girl Squad —— 最小树形图
题目链接:https://vjudge.net/problem/UVA-11183 You are part of a group of n teenage girls armed with cell ...
- URAL2104. Game with a Strip(博弈)
There is a strip 1 × n with two sides. Each square of the strip (their total amount is 2n, n squares ...
- 【POJ 3107】 Godfather
[题目链接] 点击打开链接 [算法] 这题描述有些繁琐,先简化一下题意 : 对于一棵无根树,删除一个节点,使得其余的联通块中,最大的联通块最小 那么,这题就很好做了 对这棵树进行一遍DFS,求出每个节 ...