代码随想录|二叉树的递归法、迭代法、层次遍历

import javax.crypto.Cipher;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import java.util.*;

import static java.util.Collections.copy;
import static java.util.Collections.reverse;

public class Day11 {
    private class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public List<Integer> preorderTraversal(TreeNode root) {//前根遍历
        List<Integer> result=new ArrayList<>();
        preTraversal(root,result);
        return result;
    }
    public void preTraversal(TreeNode root,List<Integer> list) {
        if(root==null)
            return;
        list.add(root.val);
        preTraversal(root.left,list);
        preTraversal(root.right,list);
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        inTraversal(root,result);
        return result;
    }
    public void inTraversal(TreeNode root,List<Integer> list) {//中根遍历
        if(root==null)
            return;
        inTraversal(root.left,list);
        list.add(root.val);
        inTraversal(root.right,list);
    }
    public List<Integer> postorderTraversal(TreeNode root) {//后根遍历
        List<Integer> result=new ArrayList<>();
        postTraversal(root,result);
        return result;
    }
    public void postTraversal(TreeNode root,List<Integer> list) {
        if(root==null)
            return;
        postTraversal(root.left,list);
        postTraversal(root.right,list);
        list.add(root.val);
    }//二叉树的递归遍历


用栈来实现二叉数的迭代
    //二叉树的迭代遍历
    // 前序遍历顺序 左-中-右 入栈顺序:中-右-左
    public List<Integer> preorderTraversal2(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        if(root==null) {
            return result;
        }
        Stack<TreeNode> stack=new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode node=stack.pop();
            result.add(node.val);
            if (node.right!=null){//先进栈右节点这样就先出栈左节点
                stack.push(node.right);
            }
            if(node.left!=null){
                stack.push(node.left);
            }
        }
        return result;
    }
    // 中序遍历顺序 左-中-右 入栈顺序:左-右
    public List<Integer> inorderTraversal2(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        if(root==null)
            return result;
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null||!stack.isEmpty()){
            if(cur!=null){
                stack.push(cur);
                cur=cur.left;
            }
            else {
                cur=stack.pop();
                result.add(cur.val);
                cur=cur.right;
                }
            }
        return result;
    }
    // 后序遍历顺序 左-右-中 入栈顺序:中-左-右 出栈顺序:中-右-左, 最后翻转结果
    public List<Integer> postorderTraversal2(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        if(root==null) {
            return result;
        }
        Stack<TreeNode> stack=new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode node=stack.pop();
            result.add(node.val);
            if(node.left!=null){
                stack.push(node.left);
            }
            if (node.right!=null){
                stack.push(node.right);
            }
        }
        Collections.reverse(result);
        return result;
    }
//统一迭代法(空指针标记法)
public List<Integer> inorderTraversal3(TreeNode root) {
    List<Integer> result=new ArrayList<>();
    if(root==null)
        return result;
    TreeNode cur=root;
    Stack<TreeNode> stack=new Stack<>();
    stack.push(cur);
    while(!stack.isEmpty()){//左中右  右中左
        cur=stack.peek();
        if(cur!=null){
            stack.pop();
            if(cur.right!=null)
                stack.push(cur.right);
            stack.push(cur);
            stack.push(null);
            if (cur.left!=null)
                stack.push(cur.left);
        }
        else {
            stack.pop();//先将null弹出
            cur=stack.pop();
            result.add(cur.val);
        }

    }
    return result;
}
    public List<Integer> preorderTraversal3(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        if(root==null)
            return result;
        TreeNode cur=root;
        Stack<TreeNode> stack=new Stack<>();
        stack.push(cur);
        while(!stack.isEmpty()){//中-左-右   右左中
            cur=stack.peek();
            if(cur!=null){
                stack.pop();
                if(cur.right!=null)
                    stack.push(cur.right);
                if (cur.left!=null)
                    stack.push(cur.left);
                stack.push(cur);
                stack.push(null);

            }
            else {
                stack.pop();//先将null弹出
                cur=stack.pop();
                result.add(cur.val);
            }

        }
        return result;
    }
    public List<Integer> postorderTraversal3(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        if(root==null)
            return result;
        TreeNode cur=root;
        Stack<TreeNode> stack=new Stack<>();
        stack.push(cur);
        while(!stack.isEmpty()){//左-右-中   中右左
            cur=stack.peek();
            if(cur!=null){
                stack.pop();

                stack.push(cur);
                stack.push(null);

                if(cur.right!=null)
                    stack.push(cur.right);
                if (cur.left!=null)
                    stack.push(cur.left);
            }
            else {
                stack.pop();//先将null弹出
                cur=stack.pop();
                result.add(cur.val);
            }

        }
        return result;
    }
//给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result=new ArrayList<>();
        if (root==null)
            return result;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            List<Integer> store=new ArrayList<>();
            int size=queue.size();//用size记录上一层有几个元素,然后添加到store中
            for(int i=0;i<size;i++){
                if(queue.peek().left!=null)
                    queue.add(queue.peek().left);
                if(queue.peek().right!=null)
                    queue.add(queue.peek().right);
                store.add(queue.poll().val);
            }
            result.add(store);
        }
    return result;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值