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

递归遍历

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;
    }
}
相关推荐
计算机学姐几秒前
基于SSM的宠物领养平台
java·vue.js·spring·maven·intellij-idea·mybatis·宠物
泰山小张只吃荷园10 分钟前
期末Python复习-输入输出
java·前端·spring boot·python·spring cloud·docker·容器
Mr_Xuhhh13 分钟前
程序地址空间
android·java·开发语言·数据库
Ning_.16 分钟前
力扣第 66 题 “加一”
算法·leetcode
kitesxian16 分钟前
Leetcode437. 路径总和 III(HOT100)
算法·深度优先
YSRM19 分钟前
异或-java-leetcode
java·算法·leetcode
大明湖的狗凯.22 分钟前
MySQL 中的乐观锁与悲观锁
java·数据库·mysql
木向22 分钟前
leetcode:222完全二叉树的节点个数
算法·leetcode
谢白羽24 分钟前
深度神经网络模型压缩学习笔记三:在线量化算法和工具、实现原理和细节
笔记·学习·dnn
z2023050827 分钟前
linux之调度管理(13)- wake affine 唤醒特性
java·开发语言