C#先序遍历2叉树(非递归)
找了下先序遍历二叉树C# 实现貌似没有 顺手些了一个
大致思路是:
传入根节点,然后依次循环其子节点推入到栈中,
当推入的节点没有子节点的时候(叶子)或者所有子节点均已经遍历过后(上一次遍历的节点是该节点的右子节点),再依次退出栈。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Node treeRoot = CreateTree();
scanTree(treeRoot);
} private static void scanTree(Node treeRoot)
{
List<Node> list = new List<Node>();
list.Add(treeRoot);
Node point = treeRoot;
Write(treeRoot);
while (true)
{
if (!list.Contains(point))
{ //上一轮是移除的操作
if (treeRoot.leftSon == point)
{//移除的是左结点
if (treeRoot.rightSon != null)
{
treeRoot = treeRoot.rightSon;
list.Add(treeRoot);
Write(treeRoot);
point = treeRoot;
continue;
}
list.Remove(treeRoot);
if (list.Count == )
{
break;
}
point = treeRoot;
treeRoot = list[list.Count - ];
}
else
{//移除的是右结点
list.Remove(treeRoot);
if (list.Count == )
{
break;
}
point = treeRoot;
treeRoot = list[list.Count - ];
}
continue;
} if (treeRoot.leftSon != null)
{
treeRoot = treeRoot.leftSon;
Write(treeRoot);
list.Add(treeRoot);
point = treeRoot;
continue;
}
if (treeRoot.rightSon != null)
{
treeRoot = treeRoot.rightSon;
Write(treeRoot);
point = treeRoot;
list.Add(treeRoot);
continue;
}
//当前的节点是叶子节点 if (treeRoot.leftSon == null && treeRoot.rightSon == null)
//{
list.Remove(treeRoot);
if (list.Count == )
{
break;
}
point = treeRoot;
treeRoot = list[list.Count - ];
// }
} } public static void Write(Node node)
{
Console.WriteLine(node.Data);
} private static Node CreateTree()
{
Node a = new Node("A");
a.leftSon = new Node("B");
a.rightSon = new Node("C"); a.leftSon.leftSon = new Node("D");
a.leftSon.rightSon = new Node("E"); a.rightSon.leftSon = new Node("F");
a.rightSon.rightSon = new Node("G"); a.leftSon.leftSon.leftSon = new Node("H");
a.leftSon.leftSon.rightSon = new Node("I");
return a;
}
} class Node
{
public string Data { get; set; }
public Node leftSon { get; set; }
public Node rightSon { get; set; } public Node(string data)
{
Data = data;
}
}
}
C#先序遍历2叉树(非递归)的更多相关文章
- 图的深度优先遍历(DFS) c++ 非递归实现
深搜算法对于程序员来讲是必会的基础,不仅要会,更要熟练.ACM竞赛中,深搜也牢牢占据着很重要的一部分.本文用显式栈(非递归)实现了图的深度优先遍历,希望大家可以相互学习. 栈实现的基本思路是将一个节点 ...
- [转载]Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)
本文主要解决一个问题,如何实现二叉树的前中后序遍历,有两个要求: 1. O(1)空间复杂度,即只能使用常数空间: 2. 二叉树的形状不能被破坏(中间过程允许改变其形状). 通常,实现二叉树的前序(pr ...
- Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)——无非是在传统遍历过程中修改叶子结点加入后继结点信息(传统是stack记录),然后再删除恢复
先看看线索二叉树 n个结点的二叉链表中含有n+1(2n-(n-1)=n+1)个空指针域.利用二叉链表中的空指针域,存放指向结点在某种遍历次序下的前驱和后继结点的指针(这种附加的指针称为"线索 ...
- [LeetCode]105. 从前序与中序遍历序列构造二叉树(递归)、108. 将有序数组转换为二叉搜索树(递归、二分)
题目 05. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 题解 使用HashMap记录当前子树根节点在中序遍历中的位置,方便每次 ...
- LeetCode 145. 二叉树的后序遍历 (用栈实现后序遍历二叉树的非递归算法)
题目链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/ 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [ ...
- C# 遍历文件夹非递归实现(采用队列的广度优先算法)(转)
一.实现思路: 1. 创建一个队列(使用C# 队列类 Queue,需要使用命名空间 System.Collections.Generic): 2. 把起始文件夹名称排入队中: 3. 检查队列中是否有文 ...
- Morris Traversal 方法遍历二叉树(非递归、不用栈,O(1)空间)
http://www.cnblogs.com/AnnieKim/archive/2013/06/15/MorrisTraversal.html
- 左神算法基础班4_1&2实现二叉树的先序、中序、后序遍历,包括递归方式和非递归
Problem: 实现二叉树的先序.中序.后序遍历,包括递归方式和非递归方式 Solution: 切记递归规则: 先遍历根节点,然后是左孩子,右孩子, 根据不同的打印位置来确定中序.前序.后续遍历. ...
- 二叉树的创建、遍历(递归和非递归实现)、交换左右子数、求高度(c++实现)
要求:以左右孩子表示法实现链式方式存储的二叉树(lson—rson),以菜单方式设计并完成功能任务:建立并存储树.输出前序遍历结果.输出中序遍历结果.输出后序遍历结果.交换左右子树.统计高度,其中对于 ...
随机推荐
- NOIP2013D1T3货车运输
题目链接:http://www.luogu.org/problem/show?pid=1967 数据:http://www.cnblogs.com/wanglichao/p/5592058.html ...
- Mysql创建新用户方法
1. CREATE USER 语法: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 例子: CREATE USER 'dog'@'lo ...
- 【xcode】qt程序不通过qmake,运行找不到动态库的坑
现象:试图在一个已有项目里增加qt的代码,因此手动加入相关framework(未通过qmake生成工程),编译连接都通过,但是运行时崩溃,提示错误: dyld: Library not loaded ...
- Java_Map_Map详解
本博客为子墨原创,转载请注明出处! http://blog.csdn.net/zimo2013/article/details/8867065 1.Map概述 Map<K,V> Map集 ...
- selenium项目的实战经验
以前学习selenium,最接近项目的经验就是写了百度首页和自己开发的一个小网站的脚本,当时觉得差不多可以了.然而这次项目实战才发现还是学到不少知识,毕竟这个网站的专业程度远超过我自己写的,而且复杂程 ...
- vuejsLearn--- -- 怎么查看、修改、追加数据---->data对象
实例观察的数据对象.可以用一个新的对象替换.实例代理了它的数据对象的属 我们现在对data2添加几项 使用数组push()追加 但是直接这样不能进行数组操作 var data2 = { city: ' ...
- 手势响应 ,避免点击多个cell同时响应同一手势多次,只响应第一个cell
http://www.cnblogs.com/wfwenchao/articles/3700205.html UIView除了负责展示内容给用户外还负责响应用户事件.本章主要介绍UIView用户交互相 ...
- ubuntu不能正常登录
在ubuntu登录界面,输入密码后,出现一个界面后一闪而过又返回登录界面.查看auth.log发现如下错误 May 15 15:42:19 tim-vm lightdm: pam_unix(light ...
- 关于python,一些整理
参数传递 1 a = 1 2 def fun(a): 3 a = 2 4 fun(a) 5 print a 6 7 # 输出: 1 a = [] def fun(a): a.append(1) fun ...
- mongoperf
官方文档 mongoperf is a utility to check disk I/O performance independently of MongoDB. It times tests o ...