代码随想录算法训练营第十四天| 144 二叉树的前序遍历 145 二叉树的后序遍历 94 二叉树的中序遍历

目录

144 二叉树的前序遍历 

递归遍历 

迭代遍历一

迭代遍历二

145 二叉树的后序遍历

 递归遍历

迭代遍历一

迭代遍历二

94 二叉树的中序遍历

 递归遍历

迭代遍历一

迭代遍历二


144 二叉树的前序遍历 

递归遍历 

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<>();
        traversal(root,res);
        return res;
    }
    void traversal(TreeNode root,ArrayList<Integer> res){
        if(root == null)return;
        res.add(root.val);//将该节点的val加入res中
        traversal(root.left,res);
        traversal(root.right,res);
    }
}

时间复杂度O(n)每个节点都被遍历一次

空间复杂度O(n) 为递归情况下栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)

迭代遍历一

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        Stack<TreeNode>stack = new Stack<>();
        ArrayList<Integer>res = new ArrayList<>();
        if(root == null)return res;
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode temp = stack.pop();
            res.add(temp.val);
            if(temp.right != null)stack.push(temp.right);
            if(temp.left != null)stack.push(temp.left);
        }
        return res;
    }
}

 时间复杂度O(n)每个节点都被遍历一次

空间复杂度O(n) 为迭代过程中栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)

迭代遍历二

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode>stack = new Stack<>();
        if(root != null)stack.push(root);
        while(!stack.isEmpty()){
            TreeNode cur = stack.pop();
            if(cur != null){
                if(cur.right != null)stack.push(cur.right);
                if(cur.left != null)stack.push(cur.left);
                stack.push(cur);
                stack.push(null);
            }else{
                cur = stack.pop();
                res.add(cur.val);
            }
        }
        return res;
    }
}

时间复杂度O(n)

空间复杂度O(n) 为迭代过程中栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)

145 二叉树的后序遍历

 递归遍历

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer>res = new ArrayList<>();
        traversal(root,res);
        return res;
    }
    void traversal(TreeNode root,List<Integer> res){
        if(root == null)return;
        traversal(root.left,res);
        traversal(root.right,res);
        res.add(root.val);
    }
}

时间复杂度O(n)每个节点都被遍历一次

空间复杂度O(n) 为递归情况下栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)

迭代遍历一

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer>res = new ArrayList<>();
        Stack<TreeNode>stack = new Stack<>();
        if(root == null)return res;
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode temp = stack.pop();
            if(temp.left != null)stack.push(temp.left);
            if(temp.right != null)stack.push(temp.right);
            res.add(temp.val);
        }
        Collections.reverse(res);
        return res;
    }
}

时间复杂度O(n)每个节点都被遍历一次

空间复杂度O(n) 为迭代过程中栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)

迭代遍历二

class Solution {//左右中
    public List<Integer> postorderTraversal(TreeNode root) {
        Stack<TreeNode>stack = new Stack<>();
        ArrayList<Integer>res = new ArrayList<>();
        if(root != null)stack.push(root);
        while(!stack.isEmpty()){
            TreeNode cur = stack.pop();
            if(cur != null){
                stack.push(cur);
                stack.push(null);
                if(cur.right != null)stack.push(cur.right);
                if(cur.left != null)stack.push(cur.left);
            }else{
                cur = stack.pop();
                res.add(cur.val);
            }
        }
        return res;
    }
}

时间复杂度O(n)

空间复杂度O(n) 为迭代过程中栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)

94 二叉树的中序遍历

 递归遍历

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer>res = new ArrayList<>();
        traversal(root,res);
        return res;
    }
    void traversal(TreeNode root,List<Integer>res){
        if(root == null)return;
        traversal(root.left,res);
        res.add(root.val);
        traversal(root.right,res);
    }
}

时间复杂度O(n)每个节点都被遍历一次

空间复杂度O(n) 为递归情况下栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)

迭代遍历一

class Solution {//左中右
    public List<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer>res = new ArrayList<>();
        Stack<TreeNode>stack = new Stack<>();
        while(root != null || !stack.isEmpty()){
            while(root != null){
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            res.add(root.val);
            root = root.right;
        }
        return res;
    }
}

时间复杂度O(n)每个节点都被遍历一次

空间复杂度O(n) 为迭代过程中栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)

迭代遍历二

class Solution {//左中右
    public List<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer>res = new ArrayList<>();
        Stack<TreeNode>stack = new Stack<>();
        if(root != null)stack.push(root);
        while(!stack.isEmpty()){
            TreeNode cur = stack.pop();
            if(cur != null){
                if(cur.right != null)stack.push(cur.right);
                stack.push(cur);
                stack.push(null);
                if(cur.left != null)stack.push(cur.left);
            }else{
                cur = stack.pop();
                res.add(cur.val);
            }
        }
        return res;
    }
}

时间复杂度O(n)

空间复杂度O(n) 为迭代过程中栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值