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
    不要让执行的勤奋掩盖思考的懒惰!
  • 相关阅读:
    内部类
    三大修饰符:static、final、abstract
    面向对象三大特性
    类和方法
    Vue-创建工程+element UI
    xshell连接虚拟机较慢问题 -----已解决
    Hbase配置
    Hive的安装配置
    Hive内容+配置
    Redis全局命令
  • 原文地址:https://www.cnblogs.com/zhiyangjava/p/6726330.html
Copyright © 2011-2022 走看看