zoukankan      html  css  js  c++  java
  • 二叉树的花式遍历

    1、前序遍历

    递归:

    public class preOrder_recursion {
        public static void preOrder(BinaryTreeNode root) {
            if (root == null) {
                return;
            }
            System.out.print(root.val + " ");
            preOrder(root.left);
            preOrder(root.right);
        }
    }
    View Code

    迭代:

    import java.util.Stack;
    
    /**
     * Created by xzy on 2017/4/16.
     */
    public class preOrder_iteration {
        public static void preOrder(BinaryTreeNode node) {
            if (node == null) {
                return;
            }
            Stack<BinaryTreeNode> st = new Stack<BinaryTreeNode>();
    
            while (true) {
                while (node != null) {
                    System.out.print(node.val + " ");
                    st.push(node);
                    node = node.left;
                }
                if (st.empty()) {
                    break;
                }
                node = st.pop().right;
            }
        }
    }
    View Code

    2、中序遍历:

    递归:

    public class inOrder_recursion {
        public static void inOrder(BinaryTreeNode node){
            if (node == null) {
                return;
            }
            inOrder(node.left);
            System.out.print(node.val + " ");
            inOrder(node.right);
        }
    }
    View Code

    迭代:

    import java.util.Stack;
    
    /**
     * Created by xzy on 2017/4/16.
     */
    public class inOrder_iteration {
        public static void inOrder(BinaryTreeNode node) {
            if (node == null) {
                return;
            }
            Stack<BinaryTreeNode> st = new Stack<BinaryTreeNode>();
            while (true) {
                while (node != null) {
                    st.push(node);
                    node = node.left;
                }
                if (st.empty()) {
                    break;
                }
                node = st.pop();
                System.out.print(node.val + " ");
                node = node.right;
            }
        }
    }
    View Code

    3、后续遍历:

    递归:

    package treeOrder;
    
    /**
     * Created by xzy on 2017/4/16.
     */
    public class postOrder_recursion {
        public static void postOrder(BinaryTreeNode node) {
            if (node == null) {
                return;
            }
            postOrder(node.left);
            postOrder(node.right);
            System.out.print(node.val + " ");
        }
    }
    View Code

    迭代:

    import java.util.Stack;
    
    /**
     * Created by xzy on 2017/4/16.
     */
    public class postOrder_iteration {
        public static void postOrder(BinaryTreeNode node) {
            if (node == null) {
                return;
            }
            Stack<BinaryTreeNode> st = new Stack<BinaryTreeNode>();
            while (true) {
                while (node != null) {
                    st.push(node);
                    node = node.left;
                }
                while (!st.empty()) {
                    if (node == st.peek().left && st.peek().right != null) {
                        node = st.peek().right;
                        break;
                    } else {
                        node = st.pop();
                        System.out.print(node.val + " ");
                    }
                }
                if (st.empty()) {
                    break;
                }
            }
        }
    }
    View Code

    4、层次遍历:

    import java.util.LinkedList;
    
    /**
     * Created by xzy on 2017/4/16.
     */
    public class LevelOrder {
        public static void levelOrder(BinaryTreeNode node) {
            if (node == null) {
                return;
            }
            LinkedList<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>();
            queue.add(node);
            while (!queue.isEmpty()) {
                node = queue.poll();
                System.out.print(node.val + " ");
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
        }
    }
    View Code
    不要让执行的勤奋掩盖思考的懒惰!
  • 相关阅读:
    把打好的war包部署到虚拟机上,mysql数据库在本机上,启动后,网页查询不到数据,异常
    NAT模式下的虚拟机
    Jsp有四种属性范围和Jsp的九大内置对象
    Tomcat笔记
    视频编解码相关基础知识(一)----H.264编码和H.265编码的区别
    相机中的一些常见参数及相关概念
    Linux常用命令
    基于Linux的C编程(一)
    Shell程序设计
    Linux文本编辑器
  • 原文地址:https://www.cnblogs.com/zhiyangjava/p/6726330.html
Copyright © 2011-2022 走看看