直接在代码。稍后细说

数据结构定义:

/**
*
*/
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 遍历树节点 同时保留所有的从根到叶节点的路径的更多相关文章

  1. java遍历树(深度遍历和广度遍历

    java遍历树如现有以下一颗树:A     B          B1               B11          B2               B22     C          C ...

  2. [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 ...

  3. Java遍历树(深度优先+广度优先)

    在编程生活中,我们总会遇见树性结构,这几天刚好需要对树形结构操作,就记录下自己的操作方式以及过程.现在假设有一颗这样树,(是不是二叉树都没关系,原理都是一样的) 1.深度优先 英文缩写为DFS即Dep ...

  4. [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 ...

  5. <Interview Problem>二叉树根到叶节点求和值匹配

    题目大意:一颗二叉树,每个节点都有一个Value, 判断根节点到叶节点的路径求和值是否等于某个数Sum. 比如说如下这样一颗二叉树,76是45,21,10这条路径的求和值,77就没有满足条件的路径. ...

  6. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

    Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...

  7. C语言递归之求根到叶节点数字之和

    题目描述 给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点 ...

  8. 树——sum-root-to-leaf-numbers(根到叶节点数字之和)

    问题: Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a numb ...

  9. 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 ...

随机推荐

  1. devstack安装使用openstack常见问题与解决的方法

    声明: 本博客欢迎转发,但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结.如有雷同,实属荣幸! 安装执行create-sta ...

  2. hdu4670(树上点分治+状态压缩)

    树上路径的f(u,v)=路径上所有点的乘积. 树上每个点的权值都是由给定的k个素数组合而成的,如果f(u,v)是立方数,那么就说明f(u,v)是可行的方案. 问有多少种可行的方案. f(u,v)可是用 ...

  3. .net读取异步Post的内容

    //读取微信Post过来的XML内容                 byte[] input = HttpContext.Current.Request.BinaryRead(HttpContext ...

  4. HDU ACM 2845 Beans-&gt;动态规划

    意甲冠军: 1. 对于每一行是,对不相邻的同一时间数取: 2.它是相同的列,相邻行不能同时服用: 3.因此,我们可以得到状态方程:dp[i]=dp[i-1]>(dp[i-2]+a[i])?dp[ ...

  5. Qt数据类型转换

    把QString转换为double类型 方法1.QString str="123.45"; double val=str.toDouble(); //val=123.45 方法2. ...

  6. docker 中国站 www.dockerpool.com 报价图片下载

    为了方便一些基本的下载docker 镜像,我建立了一个docker该站 http://www.dockerpool.com 对于Docker用户提供一站式Docker镜像服务: 稳定可靠的官方镜像下载 ...

  7. Gitblit配置

    Gitblit的安装配置及访问-windows (2013-09-11 11:52:31) 转载▼   分类: android基础 Git 是现在很流行的分布式版本控制工具,github更是人人皆知. ...

  8. Windows IOT

    Windows IOT 开发入门(准备工作)   终于抽出空来了,将最近研究的东西记录下来,物联网,万物皆可联网.然后可以做到智能家居,智能生活,智能城市....一大堆.吹牛的就不说了. 在实际应用中 ...

  9. ORACLE Install (10g r2) FOR Red Hat Enterprise Linux Server release 5.5 (64 bit) (转)

    OS Info----------# cat /etc/redhat-releaseRed Hat Enterprise Linux Server release 5.5 (Tikanga)# cat ...

  10. 【原创】用Python爬取LeetCode的AC代码到Github

    在leetCode写了105道题高调膜科,考虑搬迁到自己的GitHub上,做成一个解题题库,面试的时候也可以秀一个 但是!但是! leetCode在线IDE的功能不要太舒服,我直接线上A了不少题,本地 ...