LeetCode算法题-Binary Tree Level Order Traversal II(Java实现)

嵌入式开发避坑指南:用DWT做延时函数,这几个细节不注意代码会跑飞 本文深入探讨了在Cortex-M嵌入式开发中使用DWT实现精确延时函数的关键技巧与常见陷阱。针对DWT_CYCCNT计数器应用中的内核兼容性、溢出处理、编译器优化等五大核心问题,提供了工业级稳健解决方案,帮助开发者避免代码跑飞风险,实现微秒级高精度延时控制。 阅读详情

这是悦乐书的第165次更新,第167篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第24题(顺位题号是107)。给定二叉树,返回其节点值的自下而上级别顺序遍历(即从左到右,逐层逐层)。例如:

给定二叉树[3,9,20,null,null,15,7],

    3
   / \
  9  20
     / \
    15  7

返回其自下而上的级别顺序遍历:[[15,7],[9,20],[3]]。

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

特殊情况:当传入的二叉树为空时,返回我们新定义的空List即可。

正常情况:从示例最后要求输出的结果来看,根节点是数组的最后一位元素,如果是自顶向下遍历节点,我们可以使用队列,借助其先进先出的特点,一层一层遍历节点,将每一层遍历的节点值存入List中,再将List放入List2中,最后再倒序遍历List2存入List3中,List3就是最后的结果。

public List<List<Integer>> levelOrderBottom(TreeNode root) {
    List<List<Integer>> list2 = new ArrayList<>();
    if (root == null) {
        return list2;
    }
    List<Integer> list = new ArrayList<Integer>();
    Queue<TreeNode> q = new LinkedList<>();
    q.add(root);
    while (!q.isEmpty()) {
        list = new ArrayList<Integer>();
        Queue<TreeNode> tem = new LinkedList<>();
        while (!q.isEmpty()) {
            TreeNode t = q.poll();
            list.add(t.val);
            if (t.left != null) {
                tem.add(t.left);
            }
            if (t.right != null) {
                tem.add(t.right);
            }
        }
        list2.add(list);
        q = tem;
    }
    List<List<Integer>> list3 = new ArrayList<>();
    for(int i=list2.size()-1; i >= 0; i--){
        list3.add(list2.get(i));
    }
    return list3;
}

遍历节点的处理方法和昨天那道求最长路径的写法类似,借助队列先进先出的特点,从左往右依次循环。

03 第二种解法

我们可以将第一种解法再优化下,不再创建新的队列来存新的一层的节点。和昨天那道题的写法类似,借助队列的size来作为判断条件。

public List<List<Integer>> levelOrderBottom2(TreeNode root) {
    List<List<Integer>> list2 = new ArrayList<>();
    if (root == null) {
        return list2;
    }
    List<Integer> list = new ArrayList<Integer>();
    Queue<TreeNode> q = new LinkedList<>();
    q.offer(root);
    while (!q.isEmpty()) {
        list = new ArrayList<Integer>();
        int n = q.size();
        while (n-- > 0) {
            TreeNode t = q.poll();
            list.add(t.val);
            if (t.left != null) {
                q.offer(t.left);
            }
            if (t.right != null) {
                q.offer(t.right);
            }
        }
        list2.add(list);
    }
    List<List<Integer>> list3 = new ArrayList<>();
    for(int i=list2.size()-1; i >= 0; i--){
        list3.add(list2.get(i));
    }
    return list3;
}

04 第三种解法

上面两种解法都是使用新的List来存储List2倒序遍历的值作为最后结果返回,既然是先进后出的特点,我们可以使用栈来存储每遍历一层节点存入的List,再使用栈的pop方法出栈存入新的List。

public List<List<Integer>> levelOrderBottom3(TreeNode root) {
    List<List<Integer>> list2 = new ArrayList<>();
    if (root == null) {
        return list2;
    }
    List<Integer> list = new ArrayList<Integer>();
    Stack<List<Integer>> stack = new Stack<List<Integer>>();
    Queue<TreeNode> q = new LinkedList<>();
    q.offer(root);
    while (!q.isEmpty()) {
        list = new ArrayList<Integer>();
        int n = q.size();
        while (n-- > 0) {
            TreeNode t = q.poll();
            list.add(t.val);
            if (t.left != null) {
                q.offer(t.left);
            }
            if (t.right != null) {
                q.offer(t.right);
            }
        }
        stack.add(list);
    }
    while (!stack.isEmpty()) {
        list2.add(stack.pop());
    }
    return list2;
}

05 第四种解法

上面的三种都是使用遍历的方式,那我们可不可以使用递归的方法遍历每一层的节点值?显然是可以的,我们可以使用层数作为标记,来判断现阶段处于那一层,从而来决定是新建一个List还是取已存在的List往里面存入新的节点值。

public List<List<Integer>> levelOrderBottom4(TreeNode root) {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    List<List<Integer>> node = new ArrayList<List<Integer>>();
    viewTree(root, res, 0);
    for (int i=res.size()-1; i>=0; i--) {
        node.add(res.get(i));
    }
    return node;
}

public void viewTree(TreeNode root, List<List<Integer>> res, int deep) {
    if(root == null)
        return;
    if (res.size() <= deep) {
        List<Integer> node = new ArrayList<Integer>();
        node.add(root.val);
        res.add(node);   
    } else {
        List<Integer> node = (List<Integer>)res.get(deep);
        node.add(root.val);
    }
    viewTree(root.left, res, deep+1);
    viewTree(root.right, res, deep+1);
}

从根节点开始,先递归获取根节点左子节点及其子节点的节点值,然后再递归获取根节点右子节点及其子节点的节点值。

06 验证与测试

对于上面四种解法,我们选取了一个四层的二叉树来作为测试数据,测试代码如下:

public static void main(String[] args) {
    Easy_107_BinaryTreeLevelOrderTraversalII instance = new Easy_107_BinaryTreeLevelOrderTraversalII();
    TreeNode t = new TreeNode(1);
    TreeNode t2 = new TreeNode(2);
    TreeNode t3 = new TreeNode(3);
    TreeNode t4 = new TreeNode(4);
    TreeNode t5 = new TreeNode(5);
    TreeNode t6 = new TreeNode(6);
    TreeNode t7 = new TreeNode(7);
    TreeNode t8 = new TreeNode(8);
    t.left = t2;
    t.right = t3;
    t2.left = t4;
    t2.right = t5;
    t3.left = t6;
    t3.right = t7;
    t7.left = t8;
    long start = System.nanoTime();
    List<List<Integer>> result = instance.levelOrderBottom(t);
    long end = System.nanoTime();
    System.out.println("levelOrderBottom---输出:"+result.toString()+" , 用时:"+(end-start)/1000+"微秒");
    long start2 = System.nanoTime();
    List<List<Integer>> result2 = instance.levelOrderBottom2(t);
    long end2 = System.nanoTime();
    System.out.println("levelOrderBottom2---输出:"+result2.toString()+" , 用时:"+(end2-start2)/1000+"微秒");
    long start3 = System.nanoTime();
    List<List<Integer>> result3 = instance.levelOrderBottom3(t);
    long end3 = System.nanoTime();
    System.out.println("levelOrderBottom3---输出:"+result3.toString()+" , 用时:"+(end3-start3)/1000+"微秒");
    long start4 = System.nanoTime();
    List<List<Integer>> result4 = instance.levelOrderBottom4(t);
    long end4 = System.nanoTime();
    System.out.println("levelOrderBottom4---输出:"+result4.toString()+" , 用时:"+(end4-start4)/1000+"微秒");
}

测试结果如下:

levelOrderBottom---输出:[[8], [4, 5, 6, 7], [2, 3], [1]] , 用时:446微秒
levelOrderBottom2---输出:[[8], [4, 5, 6, 7], [2, 3], [1]] , 用时:24微秒
levelOrderBottom3---输出:[[8], [4, 5, 6, 7], [2, 3], [1]] , 用时:55微秒
levelOrderBottom4---输出:[[8], [4, 5, 6, 7], [2, 3], [1]] , 用时:23微秒

因为只是采用单一数据测试,样本数量过少,无法得出太过准确的结论,但还是可以明显看出解法二和解法四其他条件一样的情况下用时是最少的。

07 小结

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

GD32F303驱动WS2812B灯带:用TIMER+PWM+DMA解放CPU,实现流畅灯效(附完整代码) 本文详细介绍了基于GD32F303的TIMER+PWM+DMA全自动控制方案,用于高效驱动WS2812B灯带。通过硬件外设协同工作,该方案不仅解放了CPU资源,还能实现复杂的灯光效果,如渐变、流水等。文章提供了完整的代码实现和性能优化技巧,适合嵌入式开发者参考。 阅读详情

相关推荐

FPGA远程升级避坑实录:用STARTUPE2原语搞定Flash时钟,K7325T实测有效

本文深入解析了FPGA远程升级中的STARTUPE2原语应用,特别针对K7325T芯片的Flash时钟问题提供了实测有效的解决方案。通过详细的原语工作机制、Vivado工程实现步骤及调试技巧,帮助工程师规避常见陷阱,提升远程升级的可靠性和效率。

weixin_42558093的博客 142

【Leetcode-Java】102. Binary Tree Level Order Traversal

1.原题 链接:https://leetcode.com/problems/binary-tree-level-order-traversal/ Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

acceptedF的博客 363

从动态规划到最优控制:Hamilton-Jacobi-Bellman (HJB) 方程的核心思想与应用

本文深入解析了Hamilton-Jacobi-Bellman (HJB) 方程的核心思想与应用,从动态规划到最优控制的演变过程。通过实际案例展示了HJB方程在机器人路径规划、工业机械臂优化等领域的实践价值,并探讨了数值求解方法和深度学习结合的神经HJB前沿进展。

weixin_28700411的博客 459

LeetCode102 Binary Tree Level Order Traversal Java

题目: Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).

云聪的技术错题集 3929

gb28181之rtp负载之h264

我们知道国标中支持的媒体流格式不止一种,其中最常见的就数PS和h264(ES流中的其中一种)了,PS流我们之前的博文中已经分享过了,本篇博文我们来一起聊聊国标中媒体流的负载为h264的细节。 我们知道h264为了方便网络传输,把原始数据分装成了多个NALU,而NALU包大小是不固定的,因此有时候一个rtp包中可以负载多个NALU,有时候一个rtp包中只负载一个NALU,有时候多个rtp包中包含的...

2093

Python爬虫:如何抓取亚马逊商品评论和评分

通过本文的学习,您已经掌握了如何使用Selenium爬取亚马逊商品的评论数据,并处理反爬虫机制。特别是在亚马逊这样的大型电商平台,商品评论和评分不仅能够反映产品的质量和顾客的体验,还可以对商家进行竞争分析。通过爬取亚马逊的商品评论,您可以获得有价值的市场信息,如产品的优缺点、消费者的反馈等。通过查看源代码,您可以发现,亚马逊的商品评论和评分数据往往通过JavaScript动态加载,这也意味着在抓取时需要使用Selenium或Playwright等工具来模拟浏览器加载。亚马逊的评论页面支持分页加载。

2201_76125261的博客 1438

leetCode解题报告之Binary Tree Level Order Traversal II,I(二叉树层次遍历)

题目: Binary Tree Level Order Traversal II(由于Binary Tree Level Order Traversal I 这个题目只是在II的基础上少了一步最后的翻转result list而已,所以我就不贴出它的代码了) Given a binary tree, return thebottom-up level ordertraversal of...

快乐de胖虎 1万+

​非线性分析-增加位移量可能出现下面错误。虽然求解无法在所有时间点完成,但在某些点上仍能够求解部分结果。请参阅“帮助系统”中的故障排除,以了解更多信息详情。由于受约束,规划求解引擎无法收敛

基于收敛状态拆分载荷步‌:在已收敛的载荷步基础上,将未收敛的关键区域拆分为更细的子步,比如在载荷突变点前设置5~10个子步,逐步加载。替换冲突约束为弱约束‌:若存在约束冲突,将刚性约束替换为弱弹簧约束(刚度设为1e-6~1e-4),既限制刚体位移,又不影响结构的变形自由度,命令流:SPRING, NODE, K, 1e-6。部分区域约束完整,但存在未被约束的刚体自由度,或约束类型与大变形/接触逻辑冲突,比如某部件的位移约束在初始阶段有效,但大变形后约束位置脱离实际受力点,导致求解器在该阶段无法收敛。

qq_15963745的博客 149

leetcode解题之107 # Binary Tree Level Order Traversal II Java版

leetcode解题之107 # Binary Tree Level Order Traversal II Java版

mine_song的博客 570

【100分】【等和子数组最小和】

逆序排序,这样能够使得在分组时优先选取较大的元素,以便将元素分配到较小的子集中,从而使得每个子集的和相对均衡。给定一个数组nums,将元素分为若干个组,使得每组和相等,求出满足条件的所有分组中,组内元素和的最小值。分成若干个组,使得每组的和相等,并求出满足条件的所有分组中,组内元素和的最小值。在递归的过程中,我们需要注意一个优化点,即避免将相同和的桶重复计算。4 个子集(5),(1,4),(2,3),(2,3)2 个子集(5, 1, 4),(2,3, 2,3)个子集,则返回子集和的最小值,如果不能,则将。

weixin_54707168的博客 1169

基于STM32L4XX、HAL库的RTL8211FSI-CG以太网收发器(PHY)驱动程序设计

一、简介:RTL8211FSI-CG是Realtek公司生产的一款高性能、低功耗的单端口10/100M以太网收发器(PHY)。它通过RMII(Reduced Media Independent Interface)或MII接口与MAC控制器连接。

zhusl6688的博客 534

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.youkuaiyun.com/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level ord

crazy_jack 9869

38.XDMA寄存器详解2-H2C、C2H通道寄存器组剖析

目录 1.上节回顾 2. H2C寄存器剖析 2.1 H2C通道标识寄存器(0x00) 2.2 H2C通道控制寄存器(0x04) 2.3 H2C通道状态寄存器(0x40) 2.4 H2C通道完成描述符数量寄存器(0x48) 2.5 H2C通道对齐寄存器(0x4C) 2.6 H2C通道查询模式回写地址寄存器(0x88、0x8C) 2.7 H2C通道中断掩码寄存器(0x90)​ 2.8 H2C通道性能监视控制寄存器(0xC0) 2.9 H2C通道周期计数性能寄存器(0xC4、0xC8) 2

一个早起的程序员 5206

LeetCode 107. Binary Tree Level Order Traversal II 树的BFS、DFS

Binary Tree Level Order Traversal II 题意 思路 代码 BFS DFS107. Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to

老夫编代码就是一把梭 852

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

[LeetCode 107] Binary Tree Level Order Traversal II

题目链接:binary-tree-level-order-traversal-ii import java.util.ArrayList; import java.util.LinkedList; import java.util.List; /** * Given a binary tree, return the bottom-up level order traversa

瓦尔登湖 1297

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

leetcode binary-tree-level-order-traversal-ii(Java)

leetcode题目 binary-tree-level-order-traversal-ii -- newcoder 42 二叉树的层次遍历 II -- leetcode 107 题目描述 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left t...

聚沙成塔 479

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

( Leetcode 107 ) Binary Tree Level Order Traversal II

题目: Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For e

willduan的博客 5986

LeetCode Binary Tree Level Order Traversal II

原题链接在这里:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ 与Binary Tree Level Order Traversal相似,只是返过来加链表。自然想到多用一个stack即可。像BFS用queue一层一层扫,出queue时添加到list里,用curCount计数,为0时表示当前level走完,li

Dylan_Java_NYC的博客 352

Leetcode-107. Binary Tree Level Order Traversal II

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发优快云,mcf171专栏。 博客链接:mcf171的博客 —————————————————————————————— Given a binary tree, return the 

mcf171的专栏 339

[Leetcode学习-java]Binary Tree Zigzag Level Order Traversal(Z型遍历二叉树)

问题: 难度:easy 说明: 输入一个二叉树,然后Z型遍历,就是先一次正向遍历,然后再一次反向遍历,如此类推。 问题链接:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 相关问题: Binary Tree Level Order Traversal & Binary Tree Level Order Traversal II(遍历二叉树):https://blog.youkuaiyun.com/qq_2

qq_28033719的博客 245

Binary Tree Level Order Traversal II -- LeetCode

Binary Tree Level Order Traversal II -- LeetCode

weixin_43418664的博客 186

LeetCode笔记:107. Binary Tree Level Order Traversal II

从小到上整理二叉树每层的节点值

Minecraft 1441

leetcode Binary Tree Level Order Traversal II题解

题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree [3,9,20,null,nu...

leo_weile的博客 191

107.Binary Tree Level Order Traversal II

107.Binary Tree Level Order Traversal II Description binary-tree-level-order-traversal-ii Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right,...

La La Land 159

Java [Leetcode 107]Binary Tree Level Order Traversal II

题目描述: Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree{3,9,20,#,#...

dkk37351的博客 157

AI编程模型实测:GLM-5.3、Kimi K3、千问Token Plan与DeepSeek V4-Pro成本效能对比

大模型编程能力(Coding Plan)正从概念验证走向真实工程落地,其核心已非参数规模或榜单分数,而是单位算力产出的可运行代码质量。理解Coding Plan需回归本质:它是一种基于上下文感知、类型安全约束与token经济性的代码生成范式,依赖语义保真度、上下文韧性与成本穿透能力三重技术支柱。在Python工程实践中,不同模型对mypy校验、pytest覆盖、AST合规性及Redis/FastAPI等真实框架的适配表现差异显著,直接影响CI/CD稳定性与人工修正成本。本文基于27个真实开发用例、4187行

aofan9566的博客 335
上一篇: LeetCode算法题-Maximum Depth of Binary Tree
下一篇: LeetCode算法题-Convert Sorted Array to Binary Search Tree(Java实现)
程序员小川
博客等级 码龄10年 47粉丝 305原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值