Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11...
The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11]. Note: The range of node's value is in the range of 32-bit signed integer. 给一个非空二叉树,返回每层的平均值组成的数组。 解法:BFS迭代,层序遍历,计算每层的平均...
public List<Double> averageOfLevels(TreeNode root) { ArrayList<Double> avg = new ArrayList<>(); Queue<TreeNode> que = new LinkedList<>(); if (root==null) return avg; que.offer(root); while (!que.isEmpty()){ double sum=0; double perAvg; int len = que.size();int tmp_len= qu...
637. Average of Levels in Binary Tree(Easy) Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on level 0 is 3, on...
Level Averages in a Binary Tree (easy) Minimum Depth of a Binary Tree (easy) Level Order Successor (easy) Connect Level Order Siblings (medium) 8. Pattern: Tree Depth First Search,树上的DFS 树形DFS基于深搜(Depth First Search (DFS))技术来实现树的遍历。 咱们可以用递归(或是显示栈,如果你...
大家好,我是拿输出博客来督促自己刷题的老三,这一节我们来刷二叉树,二叉树相关题目在面试里非常高频,而且在力扣里数量很多,足足有几百道,不要慌,我们一步步来。我的文章很长,你们 收藏一下。
=0){//if this String has more than one wordint[]spaces=newint[numSpace];int averageSpace=(maxWidth-levelLen)/numSpace;int remainSpace=maxWidth-levelLen-numSpace*averageSpace;for(int j=0;j<numSpace;j++){if(j+1<=remainSpace){spaces[j]=1+averageSpace;}else{spaces[j]=averageSpace;}...
644.Maximum-Average-Subarray-II (H) 658.Find-K-Closest-Elements (H) 1095.Find-in-Mountain-Array (TBD) 1157.Online-Majority-Element-In-Subarray (H-) 1533.Find-the-Index-of-the-Large-Integer (M) 1712.Ways-to-Split-Array-Into-Three-Subarrays (H) 1889.Minimum-Space-Wasted-From-Packaging...
int queueSize = queue.size(); //遍历队列 for (int i = 1; i <= queueSize; i++) { //取出队列的节点 TreeNode node = queue.poll(); //每层集合中加入节点 level.add(node.val); //如果当前节点左孩子不为空,左孩子入队 if (node.left != null) {...
0633 Sum of Square Numbers Go 32.2% Easy 0634 Find the Derangement of An Array 40.1% Medium 0635 Design Log Storage System 58.6% Medium 0636 Exclusive Time of Functions Go 52.0% Medium 0637 Average of Levels in Binary Tree Go 63.0% Easy 0638 Shopping Offers Go 51.5% Medium 0639 ...