二叉树的层序遍历&力扣对应题 Java

二叉树的层序遍历 Java

  • [102. 二叉树的层序遍历](#102. 二叉树的层序遍历)
    • [错误① 队列的声明](#错误① 队列的声明)
    • [错误② `List<List<Integer>>`的声明](#错误② List<List<Integer>>的声明)
  • [107. 二叉树的层序遍历 II](#107. 二叉树的层序遍历 II)
  • [199. 二叉树的右视图](#199. 二叉树的右视图)
  • 637.二叉树的层平均值
  • 429.N叉树的层序遍历
    • [注意① LeetCode中N叉树节点的定义](#注意① LeetCode中N叉树节点的定义)
    • [注意② 增强for的使用](#注意② 增强for的使用)
  • [515. 在每个树行中找最大值](#515. 在每个树行中找最大值)
    • [错误① 最值中初始值的问题](#错误① 最值中初始值的问题)
    • [错误② Math库函数 max() 、min()](#错误② Math库函数 max() 、min())

跟着代码随想录学到了二叉树层序遍历,对相关的题进行解答&总结。

102. 二叉树的层序遍历

二叉树的层序遍历是借助队列 Queue实现的。
代码注意点 ① for循环是为了保证弹出每一行不多不少的节点,
②每一行的大小通过上一步骤的队列size确定,但是要通过另一个变量保存起来,因为队列的大小是会动态变化的

java 复制代码
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<List<Integer>>();//又错了
        if(root == null) return ans;

        Queue<TreeNode> q = new LinkedList<TreeNode>(); //又错了
        q.offer(root);
        int size = 0;
        while(!q.isEmpty()) {
            size = q.size();

            List<Integer> qq = new ArrayList<>();
            for(int i = 0; i < size; i++) { 
                //这里不可以用q.size(),因为poll之后是会变的
                TreeNode tmp = q.poll();
                qq.add(tmp.val);添加的是值,不是节点
                if(tmp.left != null) q.offer(tmp.left);
                if(tmp.right != null) q.offer(tmp.right);
            }
            ans.add(qq);
        }

        return ans;
    }
}

错误① 队列的声明

java 复制代码
 Queue<TreeNode> q = new LinkedList<TreeNode>(); 

错误② List<List<Integer>>的声明

java 复制代码
List<List<Integer>> ans = new ArrayList<List<Integer>>();

107. 二叉树的层序遍历 II

思想就是,converse 普通的层序遍历的结果

java 复制代码
class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<List<Integer>>();
        if(root == null) return ans;

        Queue<TreeNode> que = new LinkedList<>();
        que.offer(root);
        int size = 1;
        while(!que.isEmpty()) {
            size = que.size();
            List<Integer> item = new ArrayList<>();

            for(int i = 0; i < size; i++) {
                TreeNode tmp = que.poll(); // poll & pop
                item.add(tmp.val);
                if(tmp.left != null) que.offer(tmp.left);
                if(tmp.right != null) que.offer(tmp.right);
            }

            ans.add(item);
        }

        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> que = new LinkedList<>();
        que.offer(root);
        int size = 1;
        while(!que.isEmpty()) {
            size = que.size();

            for(int i = 0; i < size; i++) {
                TreeNode tmp = que.poll(); // poll & pop
                if(i == size-1) {
                    ans.add(tmp.val);
                }
                if(tmp.left != null) que.offer(tmp.left);
                if(tmp.right != null) que.offer(tmp.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> que = new LinkedList<>();
        que.offer(root);
        int size = 1;

        while(!que.isEmpty()) {
            size = que.size();
            double sum = 0.0;
            
            for(int i = 0; i < size; i++) {
                TreeNode tmp = que.poll(); // poll & pop
                sum += tmp.val;
                if(tmp.left != null) que.offer(tmp.left);
                if(tmp.right != null) que.offer(tmp.right);
            }
            sum /= size;
            ans.add(sum);
        }
        return ans;
    }
}

429.N叉树的层序遍历

java 复制代码
class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> ans = new ArrayList<List<Integer>>();
        if(root == null) return ans;

        Queue<Node> q = new LinkedList<>();
        q.offer(root);
        int size = 0;

        while(!q.isEmpty()) {
            size = q.size();

            List<Integer> item = new ArrayList<>();
            for(int i = 0; i < size; i++) { 
                //这里不可以用q.size(),因为poll之后是会变的
                Node tmp = q.poll();
                item.add(tmp.val);添加的是值,不是节点

                for(Node n : tmp.children) {
                    if(n != null) {
                        q.offer(n);
                    }
                }
            }
            ans.add(item);
        }

        return ans;
    }
}

注意① LeetCode中N叉树节点的定义

java 复制代码
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};

注意② 增强for的使用

二叉树中,访问某节点a的左孩子 右孩子的方法是,a.left 和 a.right。

在N叉树中,可以利用增强for

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

java 复制代码
class Solution {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if(root == null) return ans;

        Queue<TreeNode> q = new LinkedList<TreeNode>(); 
        q.offer(root);
        int size = 0;
        while(!q.isEmpty()) {
            size = q.size();

            int max = Integer.MIN_VALUE;//错了
            for(int i = 0; i < size; i++) { 
                TreeNode tmp = q.poll();
                if(tmp.val > max) max = tmp.val;
                
                if(tmp.left != null) q.offer(tmp.left);
                if(tmp.right != null) q.offer(tmp.right);
            }
            ans.add(max);
        }

        return ans;
    }
}

错误① 最值中初始值的问题

初始值设置成Integer中的 MIN_VALUE或者MAX_VALUE,不可以是0。

java 复制代码
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;

错误② Math库函数 max() 、min()

java 复制代码
 max = Math.max(max, node.val);
相关推荐
chxii30 分钟前
5java集合框架
java·开发语言
yychen_java1 小时前
R-tree详解
java·算法·r-tree
JANYI20182 小时前
嵌入式设计模式基础--C语言的继承封装与多态
java·c语言·设计模式
MarkHard1232 小时前
Leetcode (力扣)做题记录 hot100(62,64,287,108)
算法·leetcode·职场和发展
xrkhy2 小时前
反射, 注解, 动态代理
java
Ten peaches2 小时前
Selenium-Java版(操作元素)
java·selenium·测试工具·html
lyw2056193 小时前
RabbitMQ,Kafka八股(自用笔记)
java
邹诗钰-电子信息工程3 小时前
嵌入式自学第二十一天(5.14)
java·开发语言·算法
有梦想的攻城狮3 小时前
spring中的@MapperScan注解详解
java·后端·spring·mapperscan
寒小松3 小时前
Problem E: List练习
java·数据结构·list