Java实现二叉树的遍历(层次,先中后递归/非递归)

这篇博客介绍了如何使用层次遍历和递归方式来遍历二叉树,包括先序、中序和后序三种遍历方法。提供了详细的Java代码实现,包括层次遍历的队列实现和递归遍历的先中后序遍历。对于非递归的先序和中序遍历也给出了栈的实现方式。
摘要由CSDN通过智能技术生成

层次遍历

//层次
public int[] levelOrder(TreeNode root) {
        if(root==null) return new int[0];
        List<Integer> list = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        //TreeNode node = root;
        queue.add(root);
        while(!queue.isEmpty()){
            TreeNode node = queue.poll();
            list.add(node.val);
            if(node.left!=null){
               queue.add(node.left);
            }
            if(node.right!=null){
                queue.add(node.right);
            }
        }
        int [] res = new int [list.size()];
        for(int i=0;i<res.length;i++){
            res[i]=list.get(i);
        }
        return res;
    }

递归:先中后序遍历

public void preOrder(TreeNode root){
    List<TreeNode> list = new ArrayList<>();
    if(root==null) return;
    list.add(root);
    if(root.left!=null){
        preOrder(root.left);
    }
    if(root.right!=null){
        preOrder(root.right);
    }
}
//递归 -中序
public void inOrder(TreeNode root){
    List<TreeNode> list = new ArrayList<>();
    if(root==null) return;
    if(root.left!=null){
        preOrder(root.left);
    }
    list.add(root);
    if(root.right!=null){
        preOrder(root.right);
    }
}
//递归 -后续
public void postOrder(TreeNode root){
    List<TreeNode> list = new ArrayList<>();
    if(root==null) return;
    if(root.left!=null){
        preOrder(root.left);
    }
    if(root.right!=null){
        preOrder(root.right);
    }
    list.add(root);
}

非递归:先中后序遍历

//非递归-先序
public void preOrder(TreeNode root){
    List<Integer> list = new ArrayList<>();
    Stack<TreeNode> stack = new Stack<>();
    while(root!=null||!que.isEmpty()){
        //一直往二叉树的左子树上走
        while(root!=null){
            list.add(root.val);
            stack.push(root);
            root = root.left;
        }
        //左边的节点都走完了 改变节点方向
        if(!stack.isEmpty()){
            root = stack.pop();
            root=root.right;
        }
    }
}

//非递归-中序
public void inOrder(TreeNode root){
    List<Integer> list = new ArrayList<>();//结果
    Stack<TreeNode> stack = new Stack<>();
    while(root!=null||!que.isEmpty()){
        一直向左,但是先不存入
        while(root!=null){
            stack.push(root);
            root = root.left;
        }
        //左边的节点都走完了 存入list并 改变节点方向
        if(!stack.isEmpty()){
            root = stack.pop();
            list.add(root.val);
            root=root.right;
        }
    }
}

//非递归-后序
public void postOrder(TreeNode root){
    List<Integer> list = new ArrayList<>();//结果
    Stack<TreeNode> stack = new Stack<>();
    while(root!=null||!que.isEmpty()){
        //一直向右
        while(root!=null){
            list.add(root.val)
            stack.push(root);
            root = root.right;
        }
        if(!stack.isEmpty()){
            root = stack.pop();
            root=root.left;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值