代码随想录day14(二叉树的遍历)

代码随想录day14

题目

144.二叉树的前序遍历

145.二叉树的中序遍历

94.二叉树的后序遍历

二叉树的递归遍历

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        preorder(root,list);	//前
      // inorder(root,list);    //中 
      // postorder(root,list);	//后
        return list;
    }

	/*
	*前序便利
	*/
    public void preorder(TreeNode root,List<Integer> list){
        if(root == null){
            return;
        }
        //根节点
        list.add(root.val);
        //左子树
        preorder(root.left, list);
        //右子树
        preorder(root.right, list);
    }

	/*
	*中序便利
	*/
	public void inorder(TreeNode root,List<Integer> list){
        if(root == null){
            return;
        }

        //左子树
        inorder(root.left, list);
        //根节点
        list.add(root.val);
        //右子树
        inorder(root.right, list);
        

    }

	/*
	*后序便利
	*/
	  public void postorder(TreeNode root,List<Integer> list){
        if(root == null){
            return;
        }

        
        //左子树
        postorder(root.left, list);
        //右子树
        postorder(root.right, list);
        //根节点
        list.add(root.val);

    }
}

二叉树的迭代遍历

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        preorder(root,list);	//前
      // inorder(root,list);    //中 
      // postorder(root,list);	//后
      // Collections.reverse(list);	//后序遍历时需要反转数组
        return list;
    }

	/*
	*前序便利
	*/
    public void preorder(TreeNode root,List<Integer> list){
        if(root == null){
            return;
        }
         //利用栈的先进后出,所以我们放入栈的时候需要先放入右子树,后放入左子树
       Stack<TreeNode> stack = new Stack<>();
       stack.push(root);
       while(!stack.isEmpty()){
            TreeNode node = stack.pop();
            list.add(node.val);             //获取栈顶元素
            if(node.right != null){
                stack.push(node.right);     //将右子树放入栈中
            }
            if(node.left != null){
                stack.push(node.left);      //将左子树放入栈中
            }
       }
    }

	/*
	*中序便利
	*/
	public void inorder(TreeNode root,List<Integer> list){
        if(root == null){
            return;
        }

        while(!stack.isEmpty() || node != null){
            if(node != null){
                //找到树的最左端(是遍历的第一个元素)
                //此时栈中栈底是根节点,栈顶是最左端元素,其次是最左段元素的父节点
                stack.push(node);
                node = node.left;
            }else{
                node = stack.pop();
                list.add(node.val);
                node = node.right;
            }
        }
        

    }

	/*
	*后序便利
	*/
	  public void postorder(TreeNode root,List<Integer> list){
        if(root == null){
            return;
        }

        
         //实现遍历了中右左
       Stack<TreeNode> stack = new Stack<>();
       stack.push(root);
       while (!stack.isEmpty()){
            TreeNode node = stack.pop();
            list.add(node.val);
            if (node.left != null){
                stack.push(node.left);
            }
            if (node.right != null){
                stack.push(node.right);
            }
        }

    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值