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

递归遍历

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;
    }
}
相关推荐
洛小豆5 分钟前
饭票、图书馆、GC:这样理解 Java 引用,谁还不会?
java·后端·面试
梁下轻语的秋缘12 分钟前
每日c/c++题 备战蓝桥杯 ([洛谷 P1226] 快速幂求模题解)
c++·算法·蓝桥杯
CODE_RabbitV17 分钟前
【深度强化学习 DRL 快速实践】逆向强化学习算法 (IRL)
算法
SimonLiu00921 分钟前
清理HiNas(海纳斯) Docker日志并限制日志大小
java·docker·容器
带刺的坐椅22 分钟前
开发 MCP Proxy(代理)也可以用 Solon AI MCP 哟!
java·ai·llm·solon·mcp·mcp-server·mcp-client
yuren_xia37 分钟前
Spring XML 配置
xml·java·spring
BOB-wangbaohai43 分钟前
Flowable7.x学习笔记(十四)查看部署流程Bpmn2.0-xml
xml·笔记·学习
mit6.8241 小时前
[贪心_7] 最优除法 | 跳跃游戏 II | 加油站
数据结构·算法·leetcode
keep intensify1 小时前
通讯录完善版本(详细讲解+源码)
c语言·开发语言·数据结构·算法
小鸡脚来咯1 小时前
SpringBoot 常用注解大全
java