树的基础知识和遍历实现

作者: 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;
    }
}

对比总结:

相关文章

  • 树的基础知识和遍历实现

    概念: 1.树的遍历分为前序遍历、中序遍历、后序遍历2.前中后,其实总结来说,就是指根节点的先后遍历顺序1)前序遍...

  • 看图说话之二叉树的前序,中序,后序,层次遍历方式

    二叉树的前序,中序,后序遍历的递归实现 树的遍历方式都多种,其中树的前序,中序,后序遍历方,在原理和代码实现上都有...

  • 数据结构:树的实现和遍历(c++)

    (一)二叉树的遍历——递归实现 二叉树常见的遍历方式分为前序遍历、中序遍历和后序遍历。 1 前序遍历 前序遍历也叫...

  • Java二叉树的遍历

    Java二叉树的遍历 利用递归和非递归实现二叉树的先序,中序,后序遍历以及使用队列实现二叉树的层次遍历

  • 2018-07-20二叉树广度遍历

    树的遍历,可以广度遍历也可以深度遍历 广度遍历:一层层找 代码实现: 实现结果:

  • [LeetCode] 226. Invert Binary Tr

    我们学过树的前序遍历(DFS)和层次遍历(BFS),在邓俊辉《数据结构(C++语言版)》的前序遍历和层次遍历的实现...

  • 前端常见面试题目(六)

    一、介绍下深度优先遍历和广度优先遍历,如何实现 通过用深度优先遍历和广度优先遍历对这个dom树进行查找来理解1、 ...

  • 图的深度优先遍历

    数据结构遍历的意义 树的遍历 图的遍历 树的前序遍历 图遍历和树遍历区别 知识回顾 树的深度优先遍历 普通函数和递...

  • java中如何实现重建二叉树

    java中如何实现重建二叉树 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和...

  • 二叉树的递归遍历(java版)

    1. 场景需求 二叉树如图 java中利用递归实现二叉树的各种遍历 前序遍历 中序遍历 后序遍历 3.代码实现 3...

网友评论

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

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