【随想】每日两题Day.21

目录

1.二叉树的递归遍历

前序:

后序:

中序:

思路:

2.二叉树的迭代遍历

前序:

思路:

后序:

思路:

中序:

思路:


1.二叉树的递归遍历

前序:

java 复制代码
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new LinkedList<>();
        preorder(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);
    }
}

后序:

java 复制代码
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new LinkedList<>();
        postorder(root,list);
        return 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);
    }
}

中序:

java 复制代码
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new LinkedList<>();
        inorder(root,list);
        return 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);
    }
}
思路:

递归实现二叉树遍历还是很简单的。

2.二叉树的迭代遍历

前序:

java 复制代码
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new LinkedList<>();
        Stack<TreeNode> stack = new Stack<>();
        if(root != null) stack.push(root);
        while(!stack.empty()) {
            TreeNode node = stack.pop();
            list.add(node.val);
            if(node.right != null) stack.push(node.right);
            if(node.left != null) stack.push(node.left);
        }
        return list;
    }
}
思路:

我们利用入栈结点,然后出栈时将数值存入结果数组,然后要入栈此节点的孩子结点。由于前序遍历:根 左 右

由于栈是先进后出,所以我们要先入栈右结点,后入栈左节点

后序:

java 复制代码
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new LinkedList<>();
        Stack<TreeNode> stack = new Stack<>();
        if(root != null) stack.push(root);
        while(!stack.empty()) {
            TreeNode node = stack.pop();
            list.add(node.val);
            if(node.left != null) stack.push(node.left);
            if(node.right != null) stack.push(node.right);
        }
        Collections.reverse(list);
        return list;
    }
}
思路:

根据前序遍历的特点:根 左 右

后序遍历的特点: 左 右 根

所以将左 右 入栈的顺序颠倒后,再将结果数组reverse,就完成了后序遍历的迭代

中序:

java 复制代码
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root == null){
            return result;
        }
        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();
                result.add(cur.val);
                cur = cur.right;
            }
        }
        return result;
    }
}
思路:

中序遍历的迭代与前序后序不同,因为他是左 根 右 ,所以不是遍历到就存储的。

我们用栈存储我们遍历过的结点,用来我们回溯,当cur一直向左直到为null时,我们回溯

将cur指向出栈的结点,然后加入到结果数组,再将cur指向cur.right

直到cur和stack都为空时跳出循环。

相关推荐
程序猿_极客1 小时前
【2025 年最新版】Java JDK 安装与环境配置教程(附图文超详细,Windows+macOS 通用)
java·开发语言·windows·macos·jdk
猫头虎1 小时前
macOS 双开/多开微信WeChat完整教程(支持 4.X 及以上版本)
java·vscode·macos·微信·编辑器·mac·脚本
一个不知名程序员www4 小时前
算法学习入门 --- 哈希表和unordered_map、unordered_set(C++)
c++·算法
二哈喇子!4 小时前
BOM模型
开发语言·前端·javascript·bom
二哈喇子!5 小时前
Java开发工具——IDEA(修改全局配置,提升工作效率)
java·编辑器·intellij-idea
C++ 老炮儿的技术栈5 小时前
在C++ 程序中调用被 C编译器编译后的函数,为什么要加 extern “C”声明?
c语言·c++·windows·git·vscode·visual studio
二哈喇子!5 小时前
空指针异常
开发语言
强子感冒了5 小时前
Java网络编程学习笔记,从网络编程三要素到TCP/UDP协议
java·网络·学习
咚为5 小时前
Rust Print 终极指南:从底层原理到全场景实战
开发语言·后端·rust
二哈喇子!5 小时前
SpringBoot项目右上角选择ProjectNameApplication的配置
java·spring boot