树的基础知识和遍历实现

作者: itbird01 | 来源:发表于2021-10-02 21:46 被阅读0次

    概念:

    1.树的遍历分为前序遍历、中序遍历、后序遍历
    2.前中后,其实总结来说,就是指根节点的先后遍历顺序
    1)前序遍历:先遍历根节点,然后是左子树,最后是右子树
    2)中序遍历:先遍历左子树,然后是根节点,最后是右子树
    3)后序遍历:先遍历左子树,然后是右子树,最后是根节点
    3.遍历的方式分为递归实现、迭代实现、层序实现
    层序遍历就是逐层遍历树结构
    广度优先搜索是一种广泛运用在树或图这类数据结构中,遍历或搜索的算法。 该算法从一个根节点开始,首先访问节点本身。 然后遍历它的相邻节点,其次遍历它的二级邻节点、三级邻节点,以此类推
    通常,我们使用一个叫做队列的数据结构来帮助我们做广度优先搜索

    遍历的实现:

    1.递归实现

    1)前序遍历

    class Solution {
        /**
         * 递归前序遍历
         */
        public List<Integer> preorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<Integer>();
            preOrder(root, result);
            return result;
        }
    
        private void preOrder(TreeNode root, List<Integer> result) {
            if (root == null) {
                return;
            }
            result.add(root.val);
            preOrder(root.left, result);
            preOrder(root.right, result);
        }
    }
    

    2)中序遍历

    class Solution {
        /**
         * 递归中序遍历
         */
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<Integer>();
            inOrder(root, result);
            return result;
        }
    
        private void inOrder(TreeNode root, List<Integer> result) {
            if (root == null) {
                return;
            }
            inOrder(root.left, result);
            result.add(root.val);
            inOrder(root.right, result);
        }
    }
    

    3)后序遍历

    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<Integer>();
            postOrder(root, result);
            return result;
        }
    
        private void postOrder(TreeNode root, List<Integer> result) {
            if (root == null) {
                return;
            }
            postOrder(root.left, result);
            postOrder(root.right, result);
            result.add(root.val);
        }
    }
    

    2.迭代实现

    1)前序遍历

    class Solution {
        public List<Integer> preorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<Integer>();
            Stack<TreeNode> stack = new Stack<TreeNode>();
            while (root != null || !stack.isEmpty()) {
                while (root != null) {
                    stack.push(root);
                    result.add(root.val);
                    root = root.left;
                }
    
                if (!stack.isEmpty()) {
                    root = stack.pop();
                    root = root.right;
                }
            }
            return result;
        }
    }
    

    2)中序遍历

    class Solution {
       public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<Integer>();
            Stack<TreeNode> stack = new Stack<TreeNode>();
            while (root != null || !stack.isEmpty()) {
                while (root != null) {
                    stack.push(root);
                    root = root.left;
                }
    
                if (!stack.isEmpty()) {
                    root = stack.pop();
                    result.add(root.val);
                    root = root.right;
                }
            }
            return result;
        }
    }
    

    3)后序遍历

    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            LinkedList<Integer> result = new LinkedList<Integer>();
            Stack<TreeNode> stack = new Stack<TreeNode>();
            while (root != null || !stack.isEmpty()) {
                while (root != null) {
                    stack.push(root);
                    result.addFirst(root.val);
                    root = root.right;
                }
    
                if (!stack.isEmpty()) {
                    root = stack.pop();
                    root = root.left;
                }
            }
            return result;
        }
    }
    

    3.层序实现

    class Solution {
        public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> result = new ArrayList<>();
            if (root == null) {
                return result;
            }
            // 二叉树的层序遍历,使用队列的特性,把每一层元素入队,然后进行按序出队的同时,将每个元素的左右节点入队,直到队列为null
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            while (!queue.isEmpty()) {
                // 用于存储每层元素
                List<Integer> levelValues = new ArrayList<>();
                int size = queue.size();
                // 把每一层元素入队,然后进行按序出队的同时,将每个元素的左右节点入队
                for (int i = 0; i < size; i++) {
                    TreeNode node = queue.poll();
                    levelValues.add(node.val);
                    if (node.left != null) {
                        queue.offer(node.left);
                    }
                    if (node.right != null) {
                        queue.offer(node.right);
                    }
                }
                // 将遍历到本层元素,加入到结果链表中
                result.add(levelValues);
            }
            return result;
        }
    }
    

    对比总结:

    相关文章

      网友评论

        本文标题:树的基础知识和遍历实现

        本文链接:https://www.haomeiwen.com/subject/pjrogltx.html