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 ...
随机推荐
- 各种“GND”
资料来自网上,把个人觉得靠谱的摘取下来 1.地分类: a)直流地:直流电路“地”,零电位参考点: b)交流地:交流电的零线.要与地线区别开,不过,有时候拉电入户之前会把地线和零线接在一起: c)功率地 ...
- 如何更有效的消灭watchdogs挖矿病毒?华为云DCS Redis为您支招
漏洞概述 近日,互联网出现watchdogs挖矿病毒,攻击者可以利用Redis未授权访问漏洞入侵服务器,通过内外网扫描感染更多机器.被感染的主机出现 crontab 任务异常.系统文件被删除.CPU ...
- ./configure && make && make install详解 (转)
在Linux中利用源码包安装软件最重要的就是要仔细阅读安装包当中的README INSTALL两个说明文件,这两个文件会清楚的告诉你如何可以正确的完成这个软件的安装! 我们都知道源码包安装分为这么几个 ...
- 【CTF】后续深入学习内容
1.i春秋 https://www.ichunqiu.com/course/451 搜索black hat,可以看到黑帽大会的内容.免费. 2.wireshark 基础篇 1)由于Wireshark是 ...
- Lightoj 1003 - Drunk(拓扑排序)
One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...
- Lightoj 1020 - A Childhood Game
Allice先拿,最后拿球的输. Bob先拿,最后拿球的赢. 考虑Alice先拿球,当n=1时 Alice输 记dp[1]=0; n=2, dp[2]=1 n=3, dp[3]=1 因为n=1, ...
- UVA10600 ACM Contest and Blackout —— 次小生成树
题目链接:https://vjudge.net/problem/UVA-10600 In order to prepare the “The First National ACM School Con ...
- ubuntu下nginx的安裝
本系列的lnmp的大框架基本上是按照http://www.linuxzen.com/lnmphuan-jing-da-jian-wan-quan-shou-ce-si-lnmpda-jian-yuan ...
- POJ - 1422 Air Raid(DAG的最小路径覆盖数)
1.一个有向无环图(DAG),M个点,K条有向边,求DAG的最小路径覆盖数 2.DAG的最小路径覆盖数=DAG图中的节点数-相应二分图中的最大匹配数 3. /* 顶点编号从0开始的 邻接矩阵(匈牙利算 ...
- Local Databases with SQLiteOpenHelper
Overview For maximum control over local data, developers can use SQLite directly by leveraging SQLit ...