23-25.二叉树前序遍历、中序遍历和后续遍历

23.二叉树的前序遍历

在这里插入图片描述
在这里插入图片描述

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型一维数组
     */
    public static int[] nums = new int[101];
    public void intial (int[] nums){
        for(int i = 0; i < nums.length; i++){
           nums[i] = -1;
        }
    }
    
    int index = 0;
    public int[] preorderTraversal (TreeNode root) {
        // write code here
        intial(nums);
        pre(root);
        int[] res = new int[index];
        for(int i = 0; i < index; i++){
            res[i] = nums[i];
        }
        return res;
    }
    public void pre(TreeNode root){
        if(root==null) return;
        nums[index++] = root.val;
        pre(root.left);
        pre(root.right);
        return;
    }
}

不能在原函数直接递归是因为int数组不能扩充,所以一旦定义之后,长度就固定了,对于返回数组的操作必须在另一个函数中,可以用arraylist,但我还是用数组。递归的空间复杂度是O(N),因为最坏情况下退化成链表,时间复杂度也是O(N).

另一种方法是用栈。每次弹出栈顶元素的同时要把它的两个子节点压入栈。

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型一维数组
     */
    public int[] preorderTraversal (TreeNode root) {
        // write code here
        ArrayList<Integer> array = new ArrayList<Integer>();
        Stack<TreeNode> s = new Stack<TreeNode>();
        if(root==null) return new int[0];
        s.push(root);
        while(!s.empty()){
            TreeNode node = s.pop();
            array.add(node.val);
            if(node.right!=null) s.push(node.right);//要先弹出左节点,所以要先压入右节点
            if(node.left!=null) s.push(node.left);
        }
        int[] res = new int[array.size()];
        for(int i = 0; i< array.size(); i++){
            res[i] = array.get(i);
        }
        return res;
    }
}

空间复杂度也是O(N)

24中序遍历

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型一维数组
     */
    public int[] inorderTraversal (TreeNode root) {
        // write code here
        ArrayList<Integer> array = new ArrayList<>();
        inorder(array, root);
        int[] res = new int[array.size()];
        for(int i = 0; i < array.size(); i++){
            res[i] = array.get(i);
        }
        return res;
    }
    public void inorder(ArrayList<Integer> array, TreeNode root){
        if(root==null) return;
        inorder(array, root.left);    
        array.add(root.val);  
        inorder(array, root.right);
        
        return;
    }
}

用栈的方法需要学习一下,没有特别直接,每次遍历需要遍历到没有左节点,之后再开始进去右节点,看代码比较直接:

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型一维数组
     */
    public int[] inorderTraversal (TreeNode root) {
        // write code here
        ArrayList<Integer> array = new ArrayList<>();
        Stack<TreeNode> s = new Stack<TreeNode>();
        if(root==null) return new int[0];
        while(root!=null||!s.isEmpty()){
            while(root!=null){
                s.push(root);
                root = root.left;
            }
            TreeNode node = s.pop();
            array.add(node.val);
            root = node.right;           
        }
        int[] res = new int[array.size()];
        for(int i = 0; i < array.size(); i++){
            res[i] = array.get(i);
        }
        return res;
    }

}

25 后序遍历

只需要改一下递归的顺序即可

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型一维数组
     */
    public int[] postorderTraversal (TreeNode root) {
        // write code here
        ArrayList<Integer> array = new ArrayList<>();
        postorder(array, root);
        int[] res = new int[array.size()];
        for(int i = 0; i < array.size(); i++){
            res[i] = array.get(i);
        }
        return res;
    }
    public void postorder(ArrayList<Integer> array, TreeNode root){
        if(root==null) return;
        postorder(array, root.left);       
        postorder(array, root.right);
        array.add(root.val);
        return;
    }
}

用栈的后序遍历
step 1:开辟一个辅助栈,用于记录要访问的子节点,开辟一个前序指针pre。
step 2:从根节点开始,每次优先进入每棵的子树的最左边一个节点,我们将其不断加入栈中,用来保存父问题。
step 3:弹出一个栈元素,看成该子树的根,判断这个根的右边有没有节点或是有没有被访问过,如果没有右节点或是被访问过了,可以访问这个根,并将前序节点标记为这个根。
step 4:如果没有被访问,那这个根必须入栈,进入右子树继续访问,只有右子树结束了回到这里才能继续访问根。

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型一维数组
     */
    public int[] postorderTraversal (TreeNode root) {
        // write code here
        ArrayList<Integer> array = new ArrayList<>();
        Stack<TreeNode> s = new Stack<TreeNode>();
        TreeNode pre = null;
        while(root!=null||!s.isEmpty()){
            //每次先找到最左边的节点
            while(root!=null){
                s.push(root);
                root = root.left;
            }
            //弹出栈顶
            TreeNode node = s.pop();
            //如果该元素的右边没有或者是已经访问过
            if(node.right==null||node.right==pre){
                //访问中间的节点
                array.add(node.val);
                //记录为访问过
                pre = node;
            }else{
                //该节点入栈
                s.push(node);
                //先访问右边
                root = node.right;
            }            
        }
        int[] res = new int[array.size()];
        for(int i = 0; i < array.size(); i++){
            res[i] = array.get(i);
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值