代码随想录算法训练营第十一天

LeetCode/卡码网题目:

  • [144. 二叉树的前序遍历](#144. 二叉树的前序遍历)
  • [94. 二叉树的中序遍历](#94. 二叉树的中序遍历)
  • [145. 二叉树的后序遍历](#145. 二叉树的后序遍历)
  • [102. 二叉树的层序遍历](#102. 二叉树的层序遍历)
  • 107.二叉树的层次遍历II
  • [199. 二叉树的右视图](#199. 二叉树的右视图)
  • [637. 二叉树的层平均值](#637. 二叉树的层平均值)
  • [429. N 叉树的层序遍历](#429. N 叉树的层序遍历)
  • [515. 在每个树行中找最大值](#515. 在每个树行中找最大值)
  • [116. 填充每个节点的下一个右侧节点指针](#116. 填充每个节点的下一个右侧节点指针)
  • [117. 填充每个节点的下一个右侧节点指针 II](#117. 填充每个节点的下一个右侧节点指针 II)
  • [104. 二叉树的最大深度](#104. 二叉树的最大深度)
  • [111. 二叉树的最小深度](#111. 二叉树的最小深度)

其他:

今日总结
往期打卡


144. 二叉树的前序遍历

跳转:

问题:

思路:

递归,迭代,空指针标记模拟递归

代码(递归):

java 复制代码
void handle(TreeNode root,List<Integer> list){
        if(root == null) return;
        list.add(root.val);
        handle(root.left,list); 
        handle(root.right,list);
    }
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        handle(root,ans);
        return ans;
    }

代码(迭代):

java 复制代码
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        if(root==null) return new ArrayList<>();
       Stack<TreeNode> stack = new Stack<>();
       stack.push(root);
       List<Integer> ans = new ArrayList<>();
       while(!stack.isEmpty()){
            TreeNode tmp = stack.pop();
            ans.add(tmp.val);
            if(tmp.right!=null){
                stack.push(tmp.right);
            }
             if(tmp.left!=null){
                stack.push(tmp.left);
             } 
       }
       return ans;
    }
}

代码(空指针标记模拟递归):

java 复制代码
public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if(root==null) return ans;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode tmp = stack.pop();
            if(tmp==null){
                tmp = stack.pop();
                ans.add(tmp.val);
            }else{
                if(tmp.right!=null) stack.add(tmp.right);
                if(tmp.left!=null) stack.add(tmp.left);
                stack.add(tmp);
                stack.add(null);
            }
        }
        return ans;
    }

94. 二叉树的中序遍历

跳转:

问题:

思路:

递归,迭代,空指针标记模拟递归

代码(递归):

java 复制代码
void handle(TreeNode root,List<Integer> list){
        if(root == null) return;
        handle(root.left,list);
        list.add(root.val);
        handle(root.right,list);
    }
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        handle(root,ans);
        return ans;
    }

代码(迭代):

java 复制代码
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if(root==null) return ans;
        Stack<TreeNode> stack = new Stack<>();
        while(root!=null||!stack.isEmpty()){
            if(root!=null){
                stack.push(root);
                root = root.left;
            }else{
                root = stack.pop();
                ans.add(root.val);
                root = root.right;
            }
        }
        return ans;
    }
}

代码(空指针标记模拟递归):

java 复制代码
public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if(root==null) return ans;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode tmp = stack.pop();
            if(tmp==null){
                tmp = stack.pop();
                ans.add(tmp.val);
            }else{
                if(tmp.right!=null) stack.add(tmp.right);
                stack.add(tmp);
                stack.add(null);
                if(tmp.left!=null) stack.add(tmp.left);
            }
        }
        return ans;
    }

145. 二叉树的后序遍历

跳转:

问题:

思路:

递归,迭代,空指针标记模拟递归

代码(递归):

java 复制代码
void handle(TreeNode root,List<Integer> list){
        if(root == null) return;
        handle(root.left,list);
        handle(root.right,list);
        list.add(root.val);
    }
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        handle(root,ans);
        return ans;
    }

代码(迭代):

java 复制代码
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if (root == null)
            return ans;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode tmp = stack.pop();
            ans.add(tmp.val);
            if(tmp.left!=null) stack.add(tmp.left);
            if(tmp.right!=null) stack.add(tmp.right);
        }
        Collections.reverse(ans);
        return ans;
    }

代码(空指针标记模拟递归):

java 复制代码
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if (root == null)
            return ans;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode tmp = stack.pop();
            if(tmp==null){
                tmp = stack.pop();
                ans.add(tmp.val);
            }else{
                stack.push(tmp);
                stack.push(null);
                if(tmp.right!=null) stack.push(tmp.right);
                if(tmp.left!=null) stack.push(tmp.left);
            }
        }
        return ans;
    }

102. 二叉树的层序遍历

跳转:

问题

思路:

利用队列层序遍历

代码:

java 复制代码
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        if(root==null) return ans;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            List<Integer> list = new ArrayList<>();
            int len = queue.size();
            while(len-->0){
                TreeNode tmp = queue.poll();
                list.add(tmp.val);
                if(tmp.left!=null) queue.add(tmp.left);
                if(tmp.right!=null) queue.add(tmp.right);

            }
            ans.add(list);
        }
        return ans;

    }
}

107.二叉树的层次遍历II

跳转:

问题

代码:

java 复制代码
class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        if(root==null) return ans;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            List<Integer> list = new ArrayList<>();
            int len = queue.size();
            while(len-->0){
                TreeNode tmp = queue.poll();
                list.add(tmp.val);
                if(tmp.left!=null) queue.add(tmp.left);
                if(tmp.right!=null) queue.add(tmp.right);

            }
            ans.add(list);
        }
        Collections.reverse(ans);
        return ans;
    }
}

199. 二叉树的右视图

跳转:

问题

代码:

java 复制代码
class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if(root==null) return ans;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            int right=0;
            int len = queue.size();
            for(int i=0;i<len;i++){
                TreeNode tmp = queue.poll();
                right = tmp.val;
                if(tmp.left!=null) queue.add(tmp.left);
                if(tmp.right!=null) queue.add(tmp.right);

            }
            if(len>0)
            ans.add(right);
        }
        return ans;
    }
}

637. 二叉树的层平均值

跳转:

问题

代码:

java 复制代码
class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> ans = new ArrayList<>();
        if(root==null) return ans;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            Double avg=0.0;
            int len = queue.size();
            for(int i=0;i<len;i++){
                TreeNode tmp = queue.poll();
                avg += tmp.val;
                if(tmp.left!=null) queue.add(tmp.left);
                if(tmp.right!=null) queue.add(tmp.right);

            }
            if(len!=0)
            ans.add(avg/len);
        }
        return ans;
    }
}

429. N 叉树的层序遍历

跳转:

问题

代码:

java 复制代码
class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> ans = new ArrayList<>();
        if(root==null) return ans;
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            List<Integer> list = new ArrayList<>();
            int len = queue.size();
            while(len-->0){
                Node tmp = queue.poll();
                list.add(tmp.val);
                for(Node child:tmp.children){
                    queue.add(child);
                }

            }
            ans.add(list);
        }
        return ans;
    }
}

515. 在每个树行中找最大值

跳转:

问题

代码:

java 复制代码
class Solution {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if(root==null) return ans;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            int max=Integer.MIN_VALUE;
            int len = queue.size();
            for(int i=0;i<len;i++){
                TreeNode tmp = queue.poll();
                max = Math.max(max,tmp.val);
                if(tmp.left!=null) queue.add(tmp.left);
                if(tmp.right!=null) queue.add(tmp.right);

            }
            if(len>0)
            ans.add(max);
        }
        return ans;
    }
}

116. 填充每个节点的下一个右侧节点指针

跳转:

问题

代码:

java 复制代码
class Solution {
    public Node connect(Node root) {
        if(root==null) return null;
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            int len = queue.size();
            for(int i=0;i<len;i++){
                Node tmp = queue.poll();
                if(i==len-1) tmp.next = null;
                else tmp.next = queue.peek();
                if(tmp.left!=null) queue.add(tmp.left);
                if(tmp.right!=null) queue.add(tmp.right);

            }
        }
        return root;
    }
}

117. 填充每个节点的下一个右侧节点指针 II

跳转:

问题

代码:

java 复制代码
class Solution {
    public Node connect(Node root) {
        if(root==null) return null;
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            int len = queue.size();
            for(int i=0;i<len;i++){
                Node tmp = queue.poll();
                if(i==len-1) tmp.next = null;
                else tmp.next = queue.peek();
                if(tmp.left!=null) queue.add(tmp.left);
                if(tmp.right!=null) queue.add(tmp.right);
            }
        }
        return root;
    }
}

104. 二叉树的最大深度

跳转:

问题

代码:

java 复制代码
class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int deep = 0;
        while(!queue.isEmpty()){
            int len = queue.size();
            deep ++;
            for(int i=0;i<len;i++){
                TreeNode tmp = queue.poll();
                if(tmp.left!=null) queue.add(tmp.left);
                if(tmp.right!=null) queue.add(tmp.right);
            }
        }
        return deep;
    }
}

111. 二叉树的最小深度

跳转:

问题

代码:

java 复制代码
class Solution {
    public int minDepth(TreeNode root) {
        if(root==null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int deep = 0;
        while(!queue.isEmpty()){
            int len = queue.size();
            deep ++;
            for(int i=0;i<len;i++){
                TreeNode tmp = queue.poll();
                if(tmp.left!=null) queue.add(tmp.left);
                if(tmp.right!=null) queue.add(tmp.right);
                if(tmp.left==null&&tmp.right==null) return deep;
            }
        }
        return deep;
    }
}

总结

练习了递归遍历和层序遍历,后面都是层序遍历的题,所以每题改动不大

为什么如此匆忙,因为电脑快没电了

建议去力扣自己练习,使用效果更佳

往期打卡

代码随想录算法训练营第一天

代码随想录算法训练营第二天

代码随想录算法训练营第三天

代码随想录算法训练营第四天

代码随想录算法训练营周末一

代码随想录算法训练营第五天

代码随想录算法训练营第六天

代码随想录算法训练营第七天

代码随想录算法训练营第八天

代码随想录算法训练营第九天

代码随想录算法训练营第十天

代码随想录算法训练营周末二

*[102. 二叉树的层序遍历]: LeetCode
*[117. 填充每个节点的下一个右侧节点指针 II]: LeetCode
*[107. 二叉树的层序遍历 II]: LeetCode
*[144. 二叉树的前序遍历]: LeetCode
*[111. 二叉树的最小深度]: LeetCode
*[637. 二叉树的层平均值]: LeetCode
*[515. 在每个树行中找最大值]: LeetCode
*[94. 二叉树的中序遍历]: LeetCode
*[429. N 叉树的层序遍历]: LeetCode
*[116. 填充每个节点的下一个右侧节点指针]: LeetCode
*[199. 二叉树的右视图]: LeetCode
*[145. 二叉树的后序遍历]: LeetCode
*[104. 二叉树的最大深度]: LeetCode

相关推荐
Miraitowa_cheems4 分钟前
LeetCode算法日记 - Day 11: 寻找峰值、山脉数组的峰顶索引
java·算法·leetcode
CoovallyAIHub26 分钟前
方案 | 动车底部零部件检测实时流水线检测算法改进
深度学习·算法·计算机视觉
CoovallyAIHub28 分钟前
方案 | 光伏清洁机器人系统详细技术实施方案
深度学习·算法·计算机视觉
lxmyzzs32 分钟前
【图像算法 - 14】精准识别路面墙体裂缝:基于YOLO12与OpenCV的实例分割智能检测实战(附完整代码)
人工智能·opencv·算法·计算机视觉·裂缝检测·yolo12
洋曼巴-young34 分钟前
240. 搜索二维矩阵 II
数据结构·算法·矩阵
楼田莉子2 小时前
C++算法题目分享:二叉搜索树相关的习题
数据结构·c++·学习·算法·leetcode·面试
pusue_the_sun3 小时前
数据结构——栈和队列oj练习
c语言·数据结构·算法··队列
大锦终3 小时前
【算法】模拟专题
c++·算法
Xの哲學3 小时前
Perf使用详解
linux·网络·网络协议·算法·架构
想不明白的过度思考者3 小时前
数据结构(排序篇)——七大排序算法奇幻之旅:从扑克牌到百亿数据的魔法整理术
数据结构·算法·排序算法