树的层次遍历|青训营日记

50 阅读2分钟

这是我参与「第五届青训营 」伴学笔记创作活动的第 12天

今天做了啥子嘞

首先狂飙太好看了,我宣布除了张若昀我又爱上了张译

637. 二叉树的层平均值

给定一个非空二叉树的根节点 root , 以数组的形式返回每一层节点的平均值。与实际答案相差 10-5 以内的答案可以被接受。

/**
 * 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<Double> averageOfLevels(TreeNode root) {
        List<Double> result = new ArrayList<>();
        if (root == null) return result;
        List<TreeNode> temp = new ArrayList<>();
        temp.add(root);
        Queue<List<TreeNode>> queue = new LinkedList<>();
        queue.offer(temp);
        while (!queue.isEmpty()){
            temp = queue.poll();
            List<TreeNode> temp2 = new ArrayList<>();
            double average = 0;
            for (int i = 0;i < temp.size();i++){
                average += temp.get(i).val;
                if (temp.get(i).left != null) temp2.add(temp.get(i).left);
                if (temp.get(i).right != null) temp2.add(temp.get(i).right);
            }
            average /= temp.size();
            result.add(average);
            if (temp2.size() == 0) return result;
            queue.offer(temp2);
        }
        return result;
    }
}

199. 二叉树的右视图

给定一个二叉树的 根节点 root,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

/**
 * 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> rightSideView(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>(); 
        if(root == null) return result;
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++){
                TreeNode temp = queue.poll();
                if (temp.left != null) queue.offer(temp.left);
                if (temp.right != null) queue.offer(temp.right);
                if (i == size - 1) result.add(temp.val);                
            }
        }
        return result;
    }
}

429. N 叉树的层序遍历

给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。

树的序列化输入是用层序遍历,每组子节点都由 null 值分隔 。

/*
// 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;
    }
};
*/

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> result = new ArrayList<>();
        if (root == null) return result;
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size  = queue.size();
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                Node temp = queue.poll();
                list.add(temp.val);
                for(int j = 0; j < temp.children.size(); j++){
                    queue.offer(temp.children.get(j));
                }
            }
            result.add(list);
        }
        return result;
    }
}

splice() 方法是 JavaScript 数组对象的内置方法。你可以使用它通过删除现有元素或将其替换为新元素来更改数组的内容。 此方法修改原始数组,并将删除的元素作为新数组返回。

一个 Promise 对象代表一个在这个 promise 被创建出来时不一定已知值的代理。它让你能够把异步操作最终的成功返回值或者失败原因和相应的处理程序关联起来。这样使得异步方法可以像同步方法那样返回值:异步方法并不会立即返回最终的值,而是会返回一个 promise,以便在未来某个时候把值交给使用者。

一个 Promise 必然处于以下几种状态之一:

  • 待定(pending) :初始状态,既没有被兑现,也没有被拒绝。
  • 已兑现(fulfilled) :意味着操作成功完成。
  • 已拒绝(rejected) :意味着操作失败。