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 ...
随机推荐
- SecureCRT学习之道:SecureCRT 常用技巧
快捷键: 1. ctrl + a : 移动光标到行首 2. ctrl + e :移动光标到行尾 3. ctrl + d :删除光标之后的一个字符 4. ctrl + w : 删除行首到当前光标所在位 ...
- 工厂方法模式(factory method pattern)
工厂方法模式相对于简单工厂模式的长处是为了更好的拓展,当假设再新加一种产品,对于简单工厂模式来说须要改动核心的工厂类,但对于工厂方法模式则不须要,在工厂方法模式中核心的工厂类不再负责创建全部产品的创建 ...
- hdu3278Puzzle
其实最终的结果无非是中间8个方块是相同的颜色,外面16个方块是另外两种颜色.那么其实可以把外面两种颜色看作是0,中间的颜色看做是1. 那么题目就变成了把那种颜色看做1,而其它两种颜色看做0,可以用最少 ...
- C该程序生成一个唯一的序列号
在实际的软件开发项目,通常,它包括产生一唯一的序列号.在本文中,一个切实可行的方案,例如,它引入了一个唯一的序列号生成过程. 本文生成的序列号的样式为:MMDDHHMINSS_XXXXXX. 程序例如 ...
- Portal.MVC
Portal.MVC Portal.MVC 简介 项目是基于MVC4+EF,带有角色,权限,用户中心及账户相关(登录,注册,修改密码,找回密码等)等基本功能.参考的开源项目nopcommerce,这是 ...
- 分布式文件系统FastDFS介绍和配置过程
http://ylw6006.blog.51cto.com/470441/948729/ 由于网站使用nfs共享方式保存用户上传的图片,附件等资料,然后通过apache下载的方式供用户访问,在网站架构 ...
- 工作经常使用的SQL整理,实战篇(一)
原文:工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实 ...
- go语言实现遍历目录,及查找特定的文件类型
// filelist.go package main import ( //"flag" "fmt" "os" "path/fi ...
- 重新想象 Windows 8 Store Apps (22) - 文件系统: 访问文件夹和文件, 通过 AQS 搜索本地文件
原文:重新想象 Windows 8 Store Apps (22) - 文件系统: 访问文件夹和文件, 通过 AQS 搜索本地文件 [源码下载] 重新想象 Windows 8 Store Apps ( ...
- LCA 学习算法 (最近的共同祖先)poj 1330
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20983 Accept ...