1 Star 0 Fork 176

pinuohan / Java-learning

forked from 程序员大彬 / Java-learning 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
二叉树前序、中序、后序、层序遍历代码实现.md 4.14 KB
一键复制 编辑 原始数据 按行查看 历史
Tysondai 提交于 2021-09-12 00:44 . update数据结构与算法

Table of Contents generated with DocToc

二叉树的先序、中序和后序属于深度优先遍历DFS,层次遍历属于广度优先遍历BFS。

image-20200704000607410

前序遍历

class Solution {
    //方法1
    public List<Integer> preorderTraversal1(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        LinkedList<TreeNode> ll = new LinkedList<>(); //类型声明List改为LinkedList,List没有addFirst()/removeFirst()方法

        while (root != null || !ll.isEmpty()) {
            while (root != null) {
                result.add(root.val);
                ll.addFirst(root);
                root = root.left;
            }
            root = ll.removeFirst();
            root = root.right;
        }

        return result;
    }
    //方法2
    public List<Integer> preorderTraversal2(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root == null) {
            return result;
        }
        
        Stack<TreeNode> s = new Stack<>();
        s.push(root);
        while(!s.isEmpty()) {
            TreeNode node = s.pop();
            result.add(node.val);
            if(node.right != null) {
                s.push(node.right);//先压右,再压左
            }
            if(node.left != null) {
                s.push(node.left);
            }
        }
        
        return result;
    }
}

中序遍历

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Deque<TreeNode> deque = new ArrayDeque<>();

        while (!deque.isEmpty() || root != null) {
            while (root != null) {
                deque.push(root);
                root = root.left;
            }
            root = deque.pop();
            res.add(root.val);
            root = root.right;
        }

        return res;
    }

后序遍历

使用 null 作为标志位,访问到 null 说明此次递归调用结束。

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new LinkedList<>();
        if (root == null) {
            return res;
        }

        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            root = stack.pop();
            if (root != null) {
                stack.push(root);//最后访问
                stack.push(null);
                if (root.right != null) {
                    stack.push(root.right);
                }
                if (root.left != null) {
                    stack.push(root.left);
                }
            } else { //值为null说明此次递归调用结束,将节点值存进结果
                res.add(stack.pop().val);
            }
        }

        return res;
    }
}

层序遍历

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        LinkedList<TreeNode> queue = new LinkedList<>();
        if (root == null) {
            return res;
        }
        queue.addLast(root);
        while (!queue.isEmpty()) {
            List<Integer> levelList = new LinkedList<>();
            int size = queue.size();
            while (size-- > 0) {
                root = queue.removeFirst();
                levelList.add(root.val);
                if (root.left != null) {
                    queue.addLast(root.left);
                }
                if (root.right != null) {
                    queue.addLast(root.right);
                }
            }
            res.add(levelList);
        }
        return res;
    }
}
Java
1
https://gitee.com/pinuohan/Java-learning.git
git@gitee.com:pinuohan/Java-learning.git
pinuohan
Java-learning
Java-learning
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891