代码随想录算法训练营第十三天(递归遍历;迭代遍历;统一迭代;层序遍历)

递归遍历

LeetCode 144. 二叉树的前序遍历

题目链接:二叉树的前序遍历题目链接

代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer>result=new ArrayList<Integer>();
        preorder(root,result);
        return result;
    }
    public void preorder(TreeNode root,List<Integer> result)
    {
        if(root==null)
            return;
        result.add(root.val);
        preorder(root.left,result);
        preorder(root.right,result);
    }
}

LeetCode 145. 二叉树的后序遍历

题目链接:二叉树的后序遍历题目链接

代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer>result=new ArrayList<Integer>();
        postorder(root,result);
        return result;
    }
    public void postorder(TreeNode root,List<Integer> result)
    {
        if(root==null)
            return;
        postorder(root.left,result);
        postorder(root.right,result);
        result.add(root.val);
    }
}

LeetCode 94. 二叉树的中序遍历

题目链接:二叉树的中序遍历

代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer>result=new ArrayList<Integer>();
        inorder(root,result);
        return result;
    }
    public void inorder(TreeNode root,List<Integer>result)
    {
        if(root==null)
            return;
        inorder(root.left,result);
        result.add(root.val);
        inorder(root.right,result);
    }
}

迭代遍历

LeetCode 144. 二叉树的前序遍历

题目链接:二叉树的前序遍历题目链接

代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        Stack<TreeNode> stack=new Stack<>();
        List<Integer>result=new ArrayList<Integer>();
        if(root==null)
            return result;
        stack.push(root);
        while(!stack.isEmpty())
        {
            TreeNode node=stack.peek();
            stack.pop();
            result.add(node.val);
            if(node.right!=null) stack.push(node.right);
            if(node.left!=null) stack.push(node.left);
        }
        return result;
    }
}

LeetCode 94. 二叉树的中序遍历

题目链接:二叉树的中序遍历

代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        Stack<TreeNode>stack=new Stack<>();
        List<Integer>result=new ArrayList<>();
        TreeNode cur=root;
        while(cur!=null ||!stack.isEmpty())
        {
            if(cur!=null)
            {
                stack.push(cur);
                cur=cur.left;
            }
            else
            {
                cur=stack.peek();
                stack.pop();
                result.add(cur.val);
                cur=cur.right;
            }
        }
        return result;
    }
}

LeetCode 145. 二叉树的后序遍历

题目链接:二叉树的后序遍历题目链接

代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        if(root==null)
            return result;
        stack.push(root);
        while(!stack.isEmpty())
        {
            TreeNode cur=stack.peek();
            stack.pop();
            result.add(cur.val);
            if(cur.left!=null)  stack.push(cur.left);
            if(cur.right!=null) stack.push(cur.right); 
        }
        Collections.reverse(result);
        return result;
    }
}

层序遍历

LeetCode 102. 二叉树的层序遍历

题目链接:二叉树的层序遍历题目链接

代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode>queue=new LinkedList<>();
        List<List<Integer>> result=new ArrayList<List<Integer>>();
        if(root==null)
            return result;
        queue.offer(root);
        while(!queue.isEmpty())
        {
            int size=queue.size();
            List<Integer> middle=new ArrayList<Integer>();
            while(size-->0)
            {
                TreeNode cur=queue.peek();
                queue.poll();
                middle.add(cur.val);
                if(cur.left!=null)  queue.offer(cur.left);
                if(cur.right!=null) queue.offer(cur.right);
            }
            result.add(middle);
        }
        return result;
    }
}
相关推荐
okseekw2 小时前
Java初学者的static探险记:原来“静态”是这么个省心玩意儿!
java
这周也會开心2 小时前
JDK1.8新增语法
java·开发语言
心随雨下2 小时前
TypeScript泛型开发常见错误解析
java·开发语言·typescript
AI脚下的巨人2 小时前
机器人逆运动学:从SVD到IK算法
算法·机器人
DonaldCen6662 小时前
Java 王者修炼手册【Mysql篇 - SQL执行存储流程】:拆解 InnoDB 存储结构与 SQL 执行流程,吃透 Buffer Pool 和 Change
java
('-')2 小时前
《从根上理解MySQL是怎样运行的》第十二章学习笔记
笔记·学习·mysql
旺仔Sec3 小时前
2025年广东省职业院校技能大赛高职组“区块链技术应用”竞赛试题(二)
java·区块链
Boop_wu3 小时前
[Java EE] 多线程编程进阶
java·数据库·java-ee
ゞ 正在缓冲99%…3 小时前
2025.9.28华为软开
算法·华为
w***37513 小时前
SpringBoot【实用篇】- 测试
java·spring boot·后端