LeetCode - Easy - 637. Average of Levels in Binary Tree

Topic

  • Tree

Description

https://leetcode.com/problems/average-of-levels-in-binary-tree/

Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10−510^{-5}105 of the actual answer will be accepted.

Example 1:

Input: root = [3,9,20,null,15,7]
Output: [3.00000,14.50000,11.00000]
Explanation: 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].

Example 2:

Input: root = [3,9,20,15,7]
Output: [3.00000,14.50000,11.00000]

Constraints:

  • The number of nodes in the tree is in the range [1,104][1, 10^4][1,104].
  • −231⩽Node.val⩽231−1-2^{31} \leqslant Node.val \leqslant 2^{31} - 1231Node.val2311

Analysis

方法一:BFS

方法二:DFS

Submission

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;import com.lun.util.BinaryTree.TreeNode;public class AverageOfLevelsInBinaryTree {// 方法一:BFSpublic List<Double> averageOfLevels(TreeNode root) {LinkedList<TreeNode> queue = new LinkedList<>();queue.offer(root);List<Double> result = new ArrayList<>();while (!queue.isEmpty()) {int size = queue.size(), originSize = size;long sum = 0;for (int i = 0; i < size; i++) {TreeNode node = queue.poll();sum += node.val;if (node.left != null)queue.offer(node.left);if (node.right != null)queue.offer(node.right);}result.add(sum * 1.0 / originSize);}return result;}// 方法二:DFSpublic List<Double> averageOfLevels2(TreeNode root) {List<long[]> levelSumAndCounts = new ArrayList<>();averageOfLevels2(root, 0, levelSumAndCounts);return levelSumAndCounts.stream().map(a -> a[0] * 1.0 / a[1]).collect(Collectors.toList());}private void averageOfLevels2(TreeNode node, int level, List<long[]> levelSumAndCounts) {if (node == null)return;if (levelSumAndCounts.size() == level) {levelSumAndCounts.add(new long[] {node.val, 1});} else {long[] temp = levelSumAndCounts.get(level);temp[0] += node.val;temp[1]++;}averageOfLevels2(node.left, level + 1, levelSumAndCounts);averageOfLevels2(node.right, level + 1, levelSumAndCounts);}
}

Test

import static org.junit.Assert.*;import java.util.List;import org.junit.Test;import com.lun.util.BinaryTree;
import com.lun.util.BinaryTree.TreeNode;public class AverageOfLevelsInBinaryTreeTest {private TreeNode bigRoot = BinaryTree.integers2BinaryTree(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2147483647,2147483647,2147483647,2147483643,2147483645,2147483646,2147483646, //2147483624,2147483646,2147483647,2147483646,2147483647,2147483647,2147483631,2147483647,2147483647,2147483647, //2147483647,2147483631,2147483647,2147483647,2147483647,2147483647,2147483646,2147483646,2147483614,2147483646, //2147483647,2147483646,2147483647,2147483646,2147483647,2147483646,2147483647,2147483646,2147483647,2147483646, //2147483647,2147483646,2147483647,2147483646,2147483647,2147483646,2147483647,2147483646,2147483647,2147483646, //2147483647,2147483644,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647, //2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647, //2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644, //2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647, //2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483642,2147483647,2147483647, //2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647, //2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647, //2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636, //2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647, //2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647, //2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647, //2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647, //2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647, //2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647, //2147483647,2147483647,2147483642,2147483642,2147483647,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483647,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483647,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483638,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483642,-2147483643,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483643, //-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483643,-2147483642,-2147483642, //-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483642,-2147483642,-2147483642,-2147483642,-2147483643,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483642,-2147483642,-2147483643,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483643,-2147483642,-2147483642,-2147483642,-2147483642,-2147483598,-2147483647,-2147483647,-2147483647, //-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647, //-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645, //-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647, //-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647, //-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647, //-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483600,-2147483646,-2147483646,-2147483646,-2147483646, //-2147483646,-2147483646,-2147483646,-2147483646,-2147483646,-2147483646,-2147483646,-2147483647,-2147483647, //-2147483525,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483647,-2147483646,-2147483647, //-2147483336);@Testpublic void test() {AverageOfLevelsInBinaryTree obj = new AverageOfLevelsInBinaryTree();double delta = 10e-5;double[] expected = {3.00000, 14.50000, 11.00000};List<Double> result1 = obj.averageOfLevels(BinaryTree.integers2BinaryTree(3, 9, 20, null, 15, 7));List<Double> result2 = obj.averageOfLevels(BinaryTree.integers2BinaryTree(3, 9, 20, 15, 7));for(int i = 0; i < expected.length; i++) {assertEquals(expected[i], result1.get(i), delta);assertEquals(expected[i], result2.get(i), delta);}List<Double> result3 = obj.averageOfLevels(BinaryTree.integers2BinaryTree(2147483647, 2147483647, 2147483647));double[] expected3 = {2147483647.00000, 2147483647.00000};for(int i = 0; i < expected3.length; i++) {assertEquals(expected3[i], result3.get(i), delta);}List<Double> result4 = obj.averageOfLevels(bigRoot);double[] expected4 = {0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000};for(int i = 0; i < expected4.length; i++) {assertEquals(expected4[i], result4.get(i), delta);}}@Testpublic void test2() {AverageOfLevelsInBinaryTree obj = new AverageOfLevelsInBinaryTree();double delta = 10e-5;double[] expected = {3.00000, 14.50000, 11.00000};List<Double> result1 = obj.averageOfLevels2(BinaryTree.integers2BinaryTree(3, 9, 20, null, 15, 7));List<Double> result2 = obj.averageOfLevels2(BinaryTree.integers2BinaryTree(3, 9, 20, 15, 7));for(int i = 0; i < expected.length; i++) {assertEquals(expected[i], result1.get(i), delta);assertEquals(expected[i], result2.get(i), delta);}List<Double> result3 = obj.averageOfLevels2(BinaryTree.integers2BinaryTree(2147483647, 2147483647, 2147483647));double[] expected3 = {2147483647.00000, 2147483647.00000};for(int i = 0; i < expected3.length; i++) {assertEquals(expected3[i], result3.get(i), delta);}List<Double> result4 = obj.averageOfLevels2(bigRoot);double[] expected4 = {0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000};for(int i = 0; i < expected4.length; i++) {assertEquals(expected4[i], result4.get(i), delta);}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/445674.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在CodeBlocks下配置GoogleTest单元测试框架

环境准备 Windows 10Code::Blocks 20.03Google Test 1.7.0CMake 3.11.0 编译GoogleTest 一、创建一个工作目录D:\gtest&#xff0c;将刚下载的Google Test 1.7.0、CMake 3.11.0的压缩包解压到刚创建的工作目录。 二、进入CMake文件夹的bin下&#xff0c;运行cmake-gui.exe&…

傻子都能看懂的马拉车Manacher

Manachers Algorithm 马拉车算法操作及原理 package advanced_001;public class Code_Manacher {public static char[] manacherString(String str) {char[] charArr str.toCharArray();char[] res new char[str.length() * 2 1];int index 0;for (int i 0; i ! res.len…

简单暴力到dp的优化(萌新篇)

想写一系列文章&#xff0c;总结一些题目&#xff0c;看看解决问题、优化方法的过程到底是什么样子的。 系列问题一&#xff1a;斐波那契数列问题 在数学上&#xff0c;斐波纳契数列以如下被以递归的方法定义&#xff1a;F(0)0&#xff0c;F(1)1, F(n)F(n-1)F(n-2)&#xff08…

LeetCode - Medium - 114. Flatten Binary Tree to Linked List

Topic TreeDepth-first Search Description https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given the root of a binary tree, flatten the tree into a “linked list”: The “linked list” should use the same TreeNode class where the right…

简单暴力到dp的优化(初级篇)

一、一维非脑残 1 一个只包含A、B和C的字符串&#xff0c;如果存在某一段长度为3的连续子串中恰好A、B和C各有一个&#xff0c;那么这个字符串就是纯净的&#xff0c;否则这个字符串就是暗黑的。例如&#xff1a;BAACAACCBAAA 连续子串"CBA"中包含了A,B,C各一个&am…

ccpc河北大学生程序设计竞赛dp小总结

近期题目来自校赛&#xff0c;赛前训练&#xff0c;省赛热身&#xff0c;河北ccpc正式比赛。 题目一&#xff1a; 题目描述&#xff1a; 由于第m个台阶上有好吃的薯条&#xff0c;所以薯片现在要爬一段m阶的楼梯. 薯片每步最多能爬k个阶梯&#xff0c;但是每到了第i个台阶&a…

第一次课 优秀作业展示

18级河北师大软件编程训练 很多同学非常认真的完成了作业&#xff0c;这里选出比较优秀的作业展示出来。 注&#xff1a;展示顺序不是排名 为了尊重同学们的劳动成果&#xff0c;并没有要代码&#xff0c;只是截图展示。 范天祚 &#xff08;傻兔子&#xff09; 熊静祎&…

二分查找及一般拓展总结

二分-不止是查找哦 二分过程&#xff1a;首先&#xff0c;假设表中元素是按升序排列&#xff0c;将表中间位置记录的关键字与查找关键字比较&#xff0c;如果两者相等&#xff0c;则查找成功&#xff1b;否则利用中间位置记录将表分成前、后两个子表&#xff0c;如果中间位置记…

排序算法基本介绍及python实现(含详细注释)

对数组排序可以说是编程基础中的基础&#xff0c;本文对八种排序方法做简要介绍并用python实现。 代码中注释很全&#xff0c;适合复习和萌新学习。这是刚入学自己写的&#xff0c;可能难免比不上标准的写法&#xff0c;但是懒得改了。 文末会放和排序相关的基本拓展总结链接…

二叉搜索树实现

本文给出二叉搜索树介绍和实现 首先说它的性质&#xff1a;所有的节点都满足&#xff0c;左子树上所有的节点都比自己小&#xff0c;右边的都比自己大。 那这个结构有什么有用呢&#xff1f; 首先可以快速二分查找。还可以中序遍历得到升序序列&#xff0c;等等。。。 基本操…

快排-荷兰国旗

在使用partition-exchange排序算法时&#xff0c;如快速排序算法&#xff0c;我们会遇到一些问题&#xff0c;比如重复元素太多&#xff0c;降低了效率&#xff0c;在每次递归中&#xff0c;左边部分是空的(没有元素比关键元素小)&#xff0c;而右边部分只能一个一个递减移动。…

时间空间复杂度概述

找个时间写一写时间复杂度和一些问题分类&#xff0c;也普及一下这方面知识。 如何衡量一个算法好坏 很显然&#xff0c;最重要的两个指标&#xff1a;需要多久可以解决问题、解决问题耗费了多少资源 那我们首先说第一个问题&#xff0c;要多长时间来解决某个问题。那我们可…

二叉树遍历算法总结

文章目录前提要素深度优先搜索DFS经典遍历算法前序遍历递归版迭代版中序遍历递归版迭代版后序遍历递归版迭代版Morris遍历算法中序遍历前序遍历后序遍历广度优先搜索BFS按层遍历参考资料前提要素 本文代码用Java实现。 //二叉树节点结构 public static class TreeNode {publi…

线段树简单实现

首先&#xff0c;线段树是一棵满二叉树。&#xff08;每个节点要么有两个孩子&#xff0c;要么是深度相同的叶子节点&#xff09; 每个节点维护某个区间&#xff0c;根维护所有的。 如图&#xff0c;区间是二分父的区间。 当有n个元素&#xff0c;初始化需要o(n)时间&#xf…

树状数组实现

树状数组能够完成如下操作&#xff1a; 给一个序列a0-an 计算前i项和 对某个值加x 时间o(logn) 注意&#xff1a;有人觉得前缀和就行了&#xff0c;但是你还要维护啊&#xff0c;改变某个值&#xff0c;一个一个改变前缀和就是o(n)了。 线段树树状数组的题就是这样&#x…

KMP子字符串匹配算法学习笔记

文章目录学习资源什么是KMP什么是前缀表为什么一定要用前缀表如何计算前缀表前缀表有什么问题使用next数组来匹配放码过来构造next数组一、初始化二、处理前后缀不相同的情况三、处理前后缀相同的情况使用next数组来做匹配代码总览测试代码时间复杂度分析学习资源 字符串&…

内存分区

之前一直比较懵&#xff0c;想想还是单独写一个短篇来记录吧 一般内存主要分为&#xff1a;代码区、常量区、静态区&#xff08;全局区&#xff09;、堆区、栈区这几个区域。 代码区&#xff1a;存放程序的代码&#xff0c;即CPU执行的机器指令&#xff0c;并且是只读的。 常…

数据结构课上笔记5

介绍了链表和基本操作 用一组物理位置任意的存储单元来存放线性表的数据元素。 这组存储单元既可以是连续的&#xff0c;也可以是不连续的&#xff0c;甚至是零散分布在内存中的任意位置上的。因此&#xff0c;链表中元素的逻辑次序和 物理次序不一定相同。 定义&#xff1a; …

Java设计模式(2 / 23):观察者模式

定义 观察者&#xff08;Observer&#xff09;模式定义了对象之间的一对多依赖&#xff0c;这样一来&#xff0c;当一个对象改变状态时&#xff0c;它的所有依赖者都会收到通知并自动更新。 OO设计原则&#xff1a;为了交互对象之间的松耦合设计而努力。 案例&#xff1a;气…