java 遍历树节点 同时保留所有的从根到叶节点的路径
直接在代码。稍后细说
数据结构定义:
/**
*
*/
package Servlet; import java.util.ArrayList;
import java.util.List; /**
* @author lei
*
*/
public class node {
private String text;
private List<node>childList;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public List<node> getChildList() {
return childList;
}
public void setChildList(List<node> childList) {
this.childList = childList;
}
public static node getInitNode()
{
node nodeA=new node();
nodeA.setText("A");
node nodeB=new node();
nodeB.setText("B");
node nodeC=new node();
nodeC.setText("C");
node nodeD=new node();
nodeD.setText("D");
node nodeE=new node();
nodeE.setText("E"); List<node>lstB=new ArrayList();
lstB.add(nodeC);
lstB.add(nodeD);
nodeB.setChildList(lstB); List<node>lstA=new ArrayList();
lstA.add(nodeB);
lstA.add(nodeE);
nodeA.setChildList(lstA);
return nodeA; }
}
遍历并保存路径
/**
*
*/
package Servlet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;
/**
* @author lei
*
*/
public class IteratorNodeTool {
Map<String,List> pathMap=new HashMap();//记录全部从根节点到叶子结点的路径
private void print(List lst)//打印出路径
{
Iterator it=lst.iterator();
while(it.hasNext())
{
node n=(node)it.next();
System.out.print(n.getText()+"-");
}
System.out.println();
}
public void iteratorNode(node n,Stack<node> pathstack)
{
pathstack.push(n);//入栈
List childlist=n.getChildList();
if(childlist==null)//没有孩子 说明是叶子结点
{
List lst=new ArrayList();
Iterator stackIt=pathstack.iterator();
while(stackIt.hasNext())
{
lst.add(stackIt.next()); }
print(lst);//打印路径
pathMap.put(n.getText(), lst);//保存路径信息
return;
}else
{
Iterator it=childlist.iterator();
while(it.hasNext())
{
node child=(node)it.next();
iteratorNode(child,pathstack);//深度优先 进入递归
pathstack.pop();//回溯时候出栈
} } }
public static void main(String[] args) {
Stack <node>pathstack=new Stack();
node n=node.getInitNode();
IteratorNodeTool tool=new IteratorNodeTool();
tool.iteratorNode(n, pathstack);
} }
版权声明:本文博客原创文章,博客,未经同意,不得转载。
java 遍历树节点 同时保留所有的从根到叶节点的路径的更多相关文章
- java遍历树(深度遍历和广度遍历
java遍历树如现有以下一颗树:A B B1 B11 B2 B22 C C ...
- [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- Java遍历树(深度优先+广度优先)
在编程生活中,我们总会遇见树性结构,这几天刚好需要对树形结构操作,就记录下自己的操作方式以及过程.现在假设有一颗这样树,(是不是二叉树都没关系,原理都是一样的) 1.深度优先 英文缩写为DFS即Dep ...
- [LeetCode] 129. Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- <Interview Problem>二叉树根到叶节点求和值匹配
题目大意:一颗二叉树,每个节点都有一个Value, 判断根节点到叶节点的路径求和值是否等于某个数Sum. 比如说如下这样一颗二叉树,76是45,21,10这条路径的求和值,77就没有满足条件的路径. ...
- [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
- C语言递归之求根到叶节点数字之和
题目描述 给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点 ...
- 树——sum-root-to-leaf-numbers(根到叶节点数字之和)
问题: Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a numb ...
- LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
随机推荐
- Cacti监控Tomcatserver实现过程
1 首先去官网上面下载通用的监控模板 一般使用TomcatStats-0.1.zip 模板居多,下载地址: http://forums.cacti.net/download/file.php?id=1 ...
- 所有城市list每次从页面花1段时间抽取后写入到数组,
所有城市list每次从页面花1段时间抽取后写入到数组,
- 学习NodeJS第一天:node.js介绍
Node.JS 前辈 C 程序猿 Ryan Dahl(http://four.livejournal.com/)工程,根据 Google 著名的开源 JavaScript 发动机 V8 对于二次开发 ...
- Android中TweenAnimation四种动画切换效果
点击每个按钮都会有对应的动画显示 activity代码: package com.tmacsky; import android.app.Activity; import android.os.Bun ...
- 第一章_servlet
[Servlet与Jsp学习指南] *学习这servlet前.需要下载servlet-api.jar,MyEclipse给自己带来的需求javaEE3.0的天赋足以使用注解获得的版本号servlet ...
- erlang集群IP及port管理
erlang集群是依靠epmd维护的,epmd是erlang集群节点间port映射的守护进程.负责维护集群内的节点连接.提供节点名称到IP地址及port的解析服务. epmd 自己定义port号 ep ...
- Ubuntu 设备 spark
周围环境: Unbunt 12.04 Hadoop 2.2.x Sprak 0.9 Scala scala-2.9.0.final.tgz 一步 1. 下载 scala 2. 解压scala,然后改动 ...
- kNN(K-Nearest Neighbor)最近的分类规则
KNN最近的规则,主要的应用领域是未知的鉴定,这一推断未知的哪一类,这样做是为了推断.基于欧几里得定理,已知推断未知什么样的特点和最亲密的事情特性: K最近的邻居(k-Nearest Neighbor ...
- UVa 11879 - Multiple of 17
称号:计算一个数字是不17倍数. 说明:串,睑板. 简单的问题,直接推论可以是. 设定 n = 10a + d:(0 ≤ d ≤ 9) a - 5d = 51a - 5n,假设n被17整除,这个数必定 ...
- hdu1394--Minimum Inversion Number(线段树求逆序数,纯为练习)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...