二叉树遍历

深度优先

前序遍历 Leetcode

    public List preorderTraversal(TreeNode root) {
        List res = new ArrayList<>();
        if (root != null) {
            res.add(root.val);
            res.addAll(preorderTraversal(root.left));
            res.addAll(preorderTraversal(root.right));
        }
        return res;
    }

中序遍历 Leetcode


后序遍历 Leetcode

    public List postorderTraversal(TreeNode root) {
        List res = new ArrayList<>();
        if (root != null) {
            res.addAll(postorderTraversal(root.left));
            res.addAll(postorderTraversal(root.right));
            res.add(root.val);
        }
        return res;
    }

广度优先

层序遍历leetcode

   public List> levelOrder(TreeNode root) {
        List> res = new ArrayList<>();
        Queue q = new LinkedList();
        if (root != null) {
            q.add(root);
            while (q.isEmpty() == false) {
                List x = new ArrayList<>();
                List y = new ArrayList<>();
                TreeNode n;
                while((n = q.poll())!= null) {
                    x.add(n);
                    y.add(n.val);
                }
                res.add(y);
                x.forEach(e->{
                    if (e.left != null) q.add(e.left);
                    if (e.right != null) q.add(e.right);
                });
            }
        }

        return res;
    }

右视图leetcode

   public List rightSideView(TreeNode root) {
        List res = new ArrayList<>();
        Queue q = new LinkedList();
        if (root != null) {
            q.add(root);
            while (q.isEmpty() == false) {
                List x = new ArrayList<>();
                List y = new ArrayList<>();
                TreeNode n;
                while((n = q.poll())!= null) {
                    x.add(n);
                    y.add(n.val);
                }
                res.add(y.get(y.size()-1));
                x.forEach(e->{
                    if (e.left != null) q.add(e.left);
                    if (e.right != null) q.add(e.right);
                });
            }
        }

        return res;

    }

锯齿形层次遍历leetcode

   public List> zigzagLevelOrder(TreeNode root) {
        List> res = new ArrayList<>();
        Queue q = new LinkedList();
        boolean left2Right = true;
        if (root != null) {
            if (left2Right) {
                q.add(root);
            }
            while (q.isEmpty() == false) {
                List x = new ArrayList<>();
                List y = new ArrayList<>();
                TreeNode n;
                while((n = q.poll())!= null) {
                    x.add(0, n);
                    y.add(n.val);
                }
                res.add(y);
                left2Right = !left2Right;
                for(TreeNode e: x) {
                    if (left2Right) {
                        if (e.left != null) q.add(e.left);
                        if (e.right != null) q.add(e.right);   
                    } else {
                        if (e.right != null) q.add(e.right);
                        if (e.left != null) q.add(e.left);
                    }
                }
            }
        }

        return res;

    }

自底向上层次遍历leetcode

    public List> levelOrderBottom(TreeNode root) {
        List> res = new ArrayList<>();
        Queue q = new LinkedList();
        if (root != null) {
            q.add(root);
            while (q.isEmpty() == false) {
                List x = new ArrayList<>();
                List y = new ArrayList<>();
                TreeNode n;
                while((n = q.poll())!= null) {
                    x.add(n);
                    y.add(n.val);
                }
                res.add(0,y);
                x.forEach(e->{
                    if (e.left != null) q.add(e.left);
                    if (e.right != null) q.add(e.right);
                });
            }
        }

        return res;
    }

你可能感兴趣的:(Algorithm,二叉树,leetcode,java)