zoukankan      html  css  js  c++  java
  • 【algorithm】二叉树的遍历

    二叉树的遍历

    二叉树用例

    代码解析:

    public class BinaryTree {
    
        static class TreeNode {
            Integer val;
            TreeNode left;
            TreeNode right;
    
            public TreeNode(Integer val) {
                this.val = val;
            }
        }
    
        public static TreeNode init(Integer[] arr, int index) {
            TreeNode node = null;
            if (index < arr.length) {
                node = new TreeNode(arr[index]);
                node.left = init(arr, 2 * index + 1);
                node.right = init(arr, 2 * index + 2);
            }
            return node;
        }
    
        private static List<Integer> list = new ArrayList<>(10);
    
        public static void main(String[] args) {
            Integer[] arr = new Integer[]{1, 3, 4, 5, 6, 7, 8};
    
            System.out.println("递归实现前序遍历:   "+ rootLeftRightRecursive(init(arr,0)));
            list.clear();
            System.out.println("非递归实现前序遍历: "+ rootLeftRightNonRecursive(init(arr,0)));
            list.clear();
    
            System.out.println();
    
            System.out.println("递归实现中序遍历:   "+ leftRootRightRecursive(init(arr,0)));
            list.clear();
            System.out.println("非递归实现中序遍历: "+ leftRootRightNonRecursive(init(arr,0)));
            list.clear();
    
            System.out.println();
    
            System.out.println("递归实现后序遍历:   "+ leftRightRootRecursive(init(arr,0)));
            list.clear();
            System.out.println("非递归实现后序遍历: "+ leftRightRootNonRecursive(init(arr,0)));
            list.clear();
    
            System.out.println();
    
            System.out.println("层次遍历: "+ levelOrder(init(arr,0)));
    
            System.out.println();
    
            System.out.println("树的深度为: "+ depth(init(arr,0)));
    
    
        }
    
    
        /**
         * 递归实现前序遍历
         * 中-左-右
         * @param node TreeNode
         * @return List
         */
        public static List rootLeftRightRecursive(TreeNode node) {
            if (null != node){
                list.add(node.val);
                rootLeftRightRecursive(node.left);
                rootLeftRightRecursive(node.right);
            }
            return list;
        }
    
        /**
         * 非递归实现前序遍历
         * 中-左-右
         * @param node TreeNode
         * @return List
         */
        public static List rootLeftRightNonRecursive(TreeNode node) {
            Stack<TreeNode> stack = new Stack<>();
            TreeNode cur = node;
    
            while (null != cur || !stack.isEmpty()) {
                if (null != cur) {
                    list.add(cur.val);
                    stack.push(cur);
                    cur = cur.left;
    
                } else {
                    cur = stack.pop();
                    cur = cur.right;
                }
            }
            return list;
        }
    
        /**
         * 递归实现中序遍历
         * 左-中-右
         * @param node TreeNode
         * @return List
         */
        public static List leftRootRightRecursive(TreeNode node) {
            if (null!=node){
                leftRootRightRecursive(node.left);
                list.add(node.val);
                leftRootRightRecursive(node.right);
            }
            return list;
        }
    
        /**
         * 非递归实现中序遍历
         * 左-中-右
         * @param node TreeNode
         * @return List
         */
        public static List leftRootRightNonRecursive(TreeNode node) {
            List<Integer> list = new ArrayList<>(10);
            Stack<TreeNode> stack = new Stack<>();
            TreeNode cur = node;
    
            while (null != cur || !stack.isEmpty()) {
                if (null != cur) {
                    stack.push(cur);
                    cur = cur.left;
                } else {
                    cur = stack.pop();
                    list.add(cur.val);
                    cur = cur.right;
                }
            }
    
            return list;
        }
    
        /**
         * 递归实现后序遍历
         * 左-右-中
         * @param node TreeNode
         * @return List
         */
        public static List leftRightRootRecursive(TreeNode node){
    
            if (null!=node){
                leftRightRootRecursive(node.left);
                leftRightRootRecursive(node.right);
                list.add(node.val);
            }
            return list;
        }
    
        /**
         * 非递归实现后序遍历
         * 左-右-中
         * @param node TreeNode
         * @return List
         */
        public static List leftRightRootNonRecursive(TreeNode node){
            if (null == node){
                return list;
            }
            Stack<TreeNode> stack = new Stack<>();
            stack.push(node);
            TreeNode cur;
    
            while (!stack.isEmpty()){
                cur = stack.pop();
                if (cur.left!=null){
                    stack.push(cur.left);
                }
                if (cur.right!=null){
                    stack.push(cur.right);
                }
                // 逆序添加
                list.add(0,cur.val);
            }
            return list;
        }
    
        /**
         * 层序遍历队列实现(广度优先算法BFS)
         * @param root TreeNode
         * @return List
         */
        public static List<List<Integer>> levelOrder(TreeNode root){
            List<List<Integer>> list = new ArrayList<>();
            if(root == null){
                return list;
            }
    
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);
    
            while(!queue.isEmpty()){
                int count = queue.size();
                List<Integer> tmpList = new ArrayList<>();
                while(count > 0){
                    TreeNode node = queue.poll();
                    tmpList.add(node.val);
                    if(node.left!=null){
                        queue.add(node.left);
                    }
                    if(node.right!=null){
                        queue.add(node.right);
                    }
                    count--;
                }
                list.add(tmpList);
            }
            return list;
        }
    
    
        /**
         * 递归实现获取树的深度
         * @param node TreeNode
         * @return int
         */
        public static int depth(TreeNode node){
            if (node == null){
                return 0;
            }
            int left = depth(node.left);
            int right = depth(node.right);
    
            return left > right ? left + 1 : right + 1;
        }
    
    }
    

    结果为:

    递归实现前序遍历:   [1, 3, 5, 6, 4, 7, 8]
    非递归实现前序遍历: [1, 3, 5, 6, 4, 7, 8]
    
    递归实现中序遍历:   [5, 3, 6, 1, 7, 4, 8]
    非递归实现中序遍历: [5, 3, 6, 1, 7, 4, 8]
    
    递归实现后序遍历:   [5, 6, 3, 7, 8, 4, 1]
    非递归实现后序遍历: [5, 6, 3, 7, 8, 4, 1]
    
    层次遍历: [[1], [3, 4], [5, 6, 7, 8]]
    
    树的深度为: 3
    
  • 相关阅读:
    九省联考2018 解题报告
    「PKUSC2018」最大前缀和(状压dp)
    「雅礼集训 2017 Day2」解题报告
    UVA10829 L-Gap Substrings(后缀数组+ST表)
    [BZOJ2738]矩阵乘法(整体二分+二维树状数组)
    「雅礼集训 2017 Day1」 解题报告
    LeetCode 190. Reverse Bits (算32次即可)
    LeetCode 437. Path Sum III (STL map前缀和)
    LeetCode 744. Find Smallest Letter Greater Than Target (时间复杂度O(n))
    LeetCode 1. Two Sum (c++ stl map)
  • 原文地址:https://www.cnblogs.com/xcmelody/p/10961097.html
Copyright © 2011-2022 走看看