【随想录】Day14—第六章 二叉树part01


题目1: 二叉树的递归遍历(前、中、后)


1- 思路

  • 递归遍历实现较为简单,实现一个递归函数即可

2- 题解

①递归——前序

class Solution {
    List<Integer> res = new ArrayList<>();
    public List<Integer> preorderTraversal(TreeNode root) {
        if(root==null){
            return res;
        }
        // 前序:中、左、右
        res.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return res;
    }
}

②递归——中序

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

②递归——后序

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

题目2: 二叉树的迭代遍历(前、中、后)


1- 思路

  • 因为是迭代遍历,因此需要借助额外的数据结构 来实现

2- 题解

①迭代——前序

因为借助了栈实现迭代遍历,因此 前序的规则是 中、左、右,则入栈的顺序为 中、右、左

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

②迭代——中序

中序遍历,即 左、右

  • 对于迭代情况下的 中序遍历而言,其思路是借助一个 cur 指针,只要当前指针所指向的不为 空 或 栈内不为空则一直迭代
  • 如果 cur 非空,则一直向左移动
  • 否则 弹出 栈中的元素,弹出后 收集结果,并向右移动。
class Solution {
    List<Integer> res = new ArrayList<>();
    public List<Integer> inorderTraversal(TreeNode root) {
        if(root==null){
            return res;
        }
        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();
                res.add(cur.val);
                cur = cur.right;
            }
        }
        return res;
    }
}

③迭代——后续

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

        }
        Collections.reverse(res);
        return res;
    }
}

题目3: 二叉树的统一迭代 (前、中、后)


1- 思路

  • 二叉树的统一迭代法,其中模板较为一致,需要改变的逻辑为 node!= null 时候的处理逻辑

2- 题解

① 前序

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if(root==null){
            return res;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode node = stack.peek();
            if(node!=null){
                // 加入顺序
                stack.pop();
                if(node.right!=null) stack.push(node.right);
                if(node.left!=null) stack.push(node.left);
                stack.push(node);
                stack.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。
            }
            else{
                stack.pop();
                node = stack.peek();
                stack.pop();
                res.add(node.val);
            }
        }
        return res;
    }
}

② 中序

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if(root==null){
            return res;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode node = stack.peek();
            if(node!=null){
                // 加入顺序
                stack.pop();
                if(node.right!=null) stack.push(node.right);
                stack.push(node);
                stack.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。
                if(node.left!=null) stack.push(node.left);

            }
            else{
                stack.pop();
                node = stack.peek();
                stack.pop();
                res.add(node.val);
            }
        }
        return res;
    }
}

③ 后序

class Solution {

    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if(root==null){
            return res;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode node = stack.peek();
            if(node!=null){
                // 加入顺序
                stack.pop();
                stack.push(node);
                stack.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。
                if(node.right!=null) stack.push(node.right);
                if(node.left!=null) stack.push(node.left);
            }
            else{
                stack.pop();
                node = stack.peek();
                stack.pop();
                res.add(node.val);
            }
        }
        return res;
    }
}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值