博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二叉树的前序、中序、后序遍历迭代实现
阅读量:4223 次
发布时间:2019-05-26

本文共 3305 字,大约阅读时间需要 11 分钟。

二叉树的前序、中序、后序遍历迭代实现

二叉树的前序遍历,迭代实现 根-左-右
思路:
1、 借用栈的结构
2、 先push(root)
3、 node = pop()
3.1、list.add( node.val )
3.1、push( node.right )
3.3、push( node.left )
4、循环步骤3直到栈空

肯定很难理解,我们一步步执行下,请看图

前序遍历迭代实现

我们来实现一下代码

public List
preorderTraversal(TreeNode root) {        if (root == null) {            return null;        }        List
list = new ArrayList
();        Stack
s = new Stack
();        s.push(root);        while (!s.isEmpty()) {            TreeNode node = s.pop();            list.add(node.val);            if (node.right != null) {                s.push(node.right);            }            if (node.left != null) {                s.push(node.left);            }        }        return list;    }

中序遍历 左-根-右
思路:
1、 借用栈的结构
2、 把root、以及root左孩子都压入栈中
2.1、node = pop()
2.2、list.add( node.val )
2.3、root = node.right
3、循环步骤2直到栈为空且root为null

中序遍历迭代实现

我们实现一下代码

public static List
inorderTraversal(TreeNode root) {        if (root == null) {            return null;        }        List
list = new ArrayList
();        Stack
s = new Stack
();        do {            while (root != null) {                s.push(root);                root = root.left;            }            if (!s.isEmpty()) {                TreeNode node = s.pop();                list.add(node.val);                root = node.right;            }        } while (!s.isEmpty() || root != null);        return list;    }

后序遍历 左-右-根
思路:
1、 借用栈的结构
2、 先push(root)
3、 node = pop()
3.1、list.add( 0 , node.val )
3.2、push( node.left )
3.3、push( node.right )
4、循环步骤3直到栈空

后序遍历迭代实现

我们实现一下代码

public static List
postorderTraversal(TreeNode root) {        if (root == null) {            return null;        }        List
list = new ArrayList
();        Stack
s = new Stack
();        s.push(root);        while( !s.isEmpty() ) {            TreeNode node = s.pop();            if(node.left != null) {                s.push(node.left);            }            if(node.right != null) {                s.push(node.right);            }            list.add(0, node.val);        }        return list;    }

补充:

TreeNode类class TreeNode {    public int val;    public TreeNode left, right;    public TreeNode(int val) {        this.val = val;        this.left = this.right = null;    }}

前序遍历,递归实现

public void preorderTraversal(TreeNode root) {        if(root != null) {            System.out.print( root.val + " " );            preorderTraversal( root.left );            preorderTraversal( root.right );        }    }

中序遍历,递归实现

public void inorderTraversal(TreeNode root) {        if(root != null) {            inorderTraversal( root.left );            System.out.print( root.val + " " );            inorderTraversal( root.right );        }    }

后序遍历,递归实现

public void postorderTraversal(TreeNode root) {        if(root != null) {            postorderTraversal( root.left );            postorderTraversal( root.right );            System.out.print( root.val + " " );        }    }

递归的实现还是比较简单的,也容易理解;
————————————————
版权声明:本文为CSDN博主「奇迹码农」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42225141/article/details/80572728

你可能感兴趣的文章
UVM:8.3.1 重载transaction
查看>>
UVM:8.3.2 重载sequence
查看>>
leetcode171.[math] Excel Sheet Column Number
查看>>
Log4j配置
查看>>
java发送https请求
查看>>
java发送https请求证书问题
查看>>
js新消息提醒
查看>>
js窗体消息提醒
查看>>
深入Hibernate映射文件(二)——<hibernate-mapping>的属性
查看>>
详解在Spring中进行集成测试
查看>>
Hibernate 的工具类
查看>>
Struts2中过滤器和拦截器的区别
查看>>
51单片机:led灯闪烁10次后熄灭
查看>>
安卓:okhttp请求,获取返回数据
查看>>
安卓:股票筛选及分析系统
查看>>
Effective Java 学习笔记一 Object的方法
查看>>
使用 ctypes 进行 Python 和 C 的混合编程
查看>>
用scikit-learn学习DBSCAN聚类
查看>>
机器学习:Python实现聚类算法(三)之总结
查看>>
使用sklearn做单机特征工程
查看>>