• 二叉树的花式遍历


    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
    不要让执行的勤奋掩盖思考的懒惰!
  • 相关阅读:
    【5集iCore3_ADP演示视频】5-5 iCore3应用开发平台示波器和信号源校准
    【5集iCore3_ADP演示视频】5-4 iCore3与应用开发平台的组装与拆卸
    【iCore3应用开发平台】发布 iCore3 应用开发平台出厂代码rev0.0.3
    【emWin】例程四:显示文本
    【iCore3应用开发平台】发布 iCore3 应用开发平台出厂代码rev0.0.2
    【5集iCore3_ADP演示视频】5-3 iCore3应用开发平台摸校准
    【5集iCore3_ADP演示视频】5-2 iCore3应用开发平台上电及注意事项
    【5集iCore3_ADP演示视频】5-1 iCore3应用开发平台开箱视频
    【emWin】例程三:显示方向的切换
    【emWin】例程二:显示“hello,world”
  • 原文地址:https://www.cnblogs.com/zhiyangjava/p/6726330.html
Copyright © 2020-2023  润新知