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 ...
随机推荐
- SQL Server Insert操作中的锁
原文:SQL Server Insert操作中的锁 这篇博文简单介绍一下在SQL Server中一条Insert语句中用到的锁. 准备数据 首先我们建立一张表Table_1,它有两列Id(bigint ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...
- 九度 题目1044:Pre-Post
转载请注明本文链接http://blog.csdn.net/yangnanhai93/article/details/40658571 题目链接:pid=1044">http://ac ...
- 【Spark亚太研究院系列丛书】Spark实战高手之路-第一章 构建Spark集群(第五步)(2)
把下载下来的"hadoop-2.2.0.tar.gz"复制到"/usr/local/hadoop/"文件夹下并解压: 改动系统配置文件,改动~/.bashrc文 ...
- Access之C#连接Access
原文:Access之C#连接Access 如果是个人用的小程序的话.一般都推荐用Sqlite和Access 使用SQlite数据库需要安装SQLite驱动,详情:SQLite之C#连接SQLite 同 ...
- Java中动态代理技术生成的类与原始类的区别 (转)
用动态代理的时候,对它新生成的类长什么样子感到好奇.有幸通过一些资料消除了心里的疑惑. 平时工作使用的Spring框架里面有一个AOP(面向切面)的机制,只知道它是把类重新生成了一遍,在切面上加上了后 ...
- JPA @PersistenceContext和@Transactional Annotation
JPA(Java Persistence API )也就是说,java存储数据API,它提供的接口更方便的存储数据,当然,经过一些复杂的,并需要使用查询操作Java Persistence query ...
- android CountDownTimer
最近进行的项目使用的定时功能,我发现了一个非常容易使用内置类CountDownTimer.当然,可以使用这种效果TimerTask + Timer为了实现.只是我个人的意见CountDownTimer ...
- 在SQLAlter在现场一定的价值
update AA set aa = replace(aa,'1234','规范') where aa like '%1234%'
- cocos2dx 3.2 定义自己使用rapidjson阅读json数据
一.说明 我在这里得到的只是一个简单的定义string和Int种类,其他数据类型可以被替换向上. 两.头文件 class JsonReadUtils { public: static JsonRead ...