144. 二叉树的前序遍历 Binary Tree Preorder Traversal

C++上机——2023年“信息与未来”小学生编程思维展示官方题解 本次活动前三题是“根据问题描述实现代码”类型的问题,题解和示例代码由人工智能GPT-4直接阅读题面后生成――只要熟悉C++的语法和基本的编程、调试,即可解决p1、p2、p3。后三题有一定的算法性,我们给出命题思路和一些可能的解法。注意:本题解是面向指导教师的;指导教师在讲解时可以根据学生掌握程度进行调整。 阅读详情
给定一个二叉树,返回它的 前序 遍历。

 示例:

输入: [1,null,2,3]  
   1
    \
     2
    /
   3 

输出: [1,2,3]
递归:

    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        preOrder(root, ret);
        return ret;
    }

    private void preOrder(TreeNode node, List<Integer> list) {
        if (node == null) {
            return;
        }
        list.add(node.val);
        preOrder(node.left, list);
        preOrder(node.right, list);
    }
迭代:

    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            ret.add(node.val);
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
        }
        return ret;
    }


YOLOv8剪枝实战:基于torch_pruning的模型优化与性能提升指南 本文详细介绍了如何使用torch_pruning工具对YOLOv8模型进行结构化剪枝,以实现模型轻量化与性能提升。通过实战步骤解析,包括环境搭建、依赖图分析、迭代剪枝与微调策略,帮助开发者有效减少模型参数量和计算量,提升在边缘设备上的推理速度,同时保持检测(detect)与分割(segment)任务的精度。 阅读详情

相关推荐

什么事LLVM和Clang?Clang与GCC有什么区别?Clang如何使用?

这个问题主要讲Xcode编译器的认知,在学习iOS之前有必要学习一下Objective-C的编译环境。 在了解LLVM和Clang之前,先得了解一下什么是编译器。 编译器是一种计算机程序,它将某种编程语言(高级编程语言)写成的源代码转换成另一种编程语言(低级编程语言)。一个现代编译器的主要工作流程为:源代码->预处理器->编译器->目标代码->链接器->可执行程序...

Welcome~ 9724

Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive solution is triv

欢迎来到饭没了吃的博客 354

从零到一:RF-DETR如何通过神经架构搜索重塑实时目标检测的边界

RF-DETR通过创新的权重共享神经架构搜索(NAS)机制,重新定义了实时目标检测的技术边界。这项技术在COCO等基准测试中表现卓越,成为首个AP超过60的实时检测器,同时保持跨领域稳定性和高效推理速度。RF-DETR的动态架构调整能力使其能够适应从嵌入式设备到云端服务器的各种硬件环境,为工业质检、自动驾驶等场景提供灵活解决方案。

weixin_29061531的博客 471

5.1.1—二叉树的遍历—Binary Tree Preorder Traversal

描述 Given a binary tree, return the preorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3}, 1  \  2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could yo

seu_nuaa_zc的博客 456

基于YOLO+DeepSeek的农作物病虫害检测系统

本系统是一个基于YOLO+DeepSeek的农作物病虫害检测系统。 可使用YOLOV1-YOLOV12的任意模型进行目标检测。可以实现检测图片、批量检测图片、视频检测、摄像头检测四种方式。

qq_51977316的博客 1810

Spring AI实现RAG(检索增强生成)详解与实践

支持多种文件格式的文档加载实现高效的向量存储和检索构建智能的问答系统提供可追溯的知识服务RAG技术为AI应用提供了强大的知识增强能力,使得模型能够基于实际文档生成准确、可靠的回答。

beiduofen2011的专栏 1240

[Leetcode] Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal

这是两道题: 102. Binary Tree Level Order Traversal 103. Binary Tree Zigzag Level Order Traversal 102这道题就是按层次遍历,但是要把每一层的节点分别存储到vector里面去。 两年前就做过这道题,还写了博客。 当时的做法是硬生生地给每个结点加了个域,用来标记高度。很蹩脚。 今天做10

新的开始 455

凝聚态物理新引擎:AI 驱动材料预测与相变识别大突破

摘要:本文深入探讨 AI 在凝聚态物理中的创新应用。在材料性质预测方面,AI 借助数据收集、特征提取和模型训练,能依据材料微观信息准确预估其宏观性质,以石墨烯为例验证了高效性。对于拓扑相变识别,AI 通过分析量子力学数据,利用机器学习算法判别相变节点,助力拓扑绝缘体研究。尽管面临数据质量、模型可解释性等挑战,但未来多模态数据融合、可解释 AI 等发展方向,将推动 AI 与凝聚态物理深度融合,助力探索物质奥秘与新材料研发。

专注于人工智能、软件开发、工控自动化、工厂数字化及智能化等领域,希望和大家共同进步! 1832

binary-tree-preorder-traversal(二叉树前序遍历)

题目描述 Given a binary tree, return the preorder traversal of its nodes’ values. For example:Given binary tree{1,#,2,3},return[1,2,3]. Recursive solution is trivial, could you do it iteratively? 翻译:给定二...

傻月菇凉的博客 2165

【毕设保姆级指南】基于YOLOv8的DAGM 2007工业缺陷检测系统从搭建到落地

本教程基于YOLOv8算法实现DAGM 2007工业缺陷检测系统,包含数据处理、模型训练和可视化界面开发全流程。首先对DAGM数据集进行格式转换和划分,然后通过YOLOv8训练工业缺陷检测模型,最终构建PyQt5交互界面实现缺陷可视化检测。该系统可用于智能制造中的产品质量控制,帮助快速定位工业产品缺陷,提升检测效率和准确性,为工业质检提供高效解决方案。

LXY_LOVE36的博客 186

深度学习实战12(进阶版)-利用Dewarp实现文本扭曲矫正

大家好,我是微学A1, 我们在生活中会看到一些拍摄扭曲的图片,我们在通过OCR识别的时候,因为扭曲的厉害,而无法识别,我们需要对图片进行处理。

微学AI的博客 3158

LeetCode144 Binary Tree Preorder Traversal 二叉树的前序遍历

Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,2,3] 题源:here;完整实现:here 思路: 二叉树的前序遍历,根据其定义编程即可:先访问跟,...

李歇特冯-兹拜因巴哈的博客 140

RV1126 ROS2环境交叉编译及部署(基于官方Docker)

我的所有基于`RV1126`的机器人项目都是在`Ubuntu22.04`下进行开发的,在`Ubuntu18.04 Docker`容器中实现`RV1126`的`SDK`编译,详细可参考我之前的博客[《Ubuntu20.04/22.04下Docker方案实现多平台SDK编译》第2章](https://blog.csdn.net/w1820020635/article/details/147342689),`RV1126`的`ROS1`部署没什么难度,可以参考我之前的博客,这里记录了`ROS2`交叉编译及问题解决

w1820020635的博客 1143

LeetCode 144 Binary Tree Preorder Traversal(二叉树的前序遍历)+(二叉树、迭代)

翻译给定一个二叉树,返回其前序遍历的节点的值。例如: 给定二叉树为 {1,#, 2, 3} 1 \ 2 / 3 返回 [1, 2, 3]备注:用递归是微不足道的,你可以用迭代来完成它吗?原文Given a binary tree, return the preorder traversal of its nodes' values.For example: Gi

nomasp 4862

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

深度学习工具包 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'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('DeepLearnToolbox'));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('state',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'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',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 = 'sigm';%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, 'Too big error');```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('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';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)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';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, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% 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'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 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, 'Too big error');```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('state',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, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',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, 'Too big error');%% ex3 neural net with dropoutrand('state',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, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % 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, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % 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, 'Too big error');%% 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('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % 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, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历

题目:给定一个二叉树,返回它的 前序 遍历。 示例:输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗?代码实现:前序遍历的通用实现可以查看博客:二叉树遍历系列--前序遍历递归版本:/** * Definition for a binary tree node. * pub...

zhangzhetaojj的博客 295

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Leetcode 144. Binary Tree Preorder Traversal二叉树前序遍历

Given a binary tree, return thepreordertraversal of its nodes' values. Example: Input:[1,null,2,3] 1 \ 2 / 3 Output:[1,2,3] 题目链接:https://leetcode.com/problems/binary-tree-p...

salmonwilliam的博客 429

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

LeetCode 144 Binary Tree Preorder Traversal(二叉树前序遍历)

Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive solution is tr

nudt_oys的博客 726

Leetcode144. Binary Tree Preorder Traversal二叉树的前序遍历

给定一个二叉树,返回它的前序遍历。 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶:递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; vector<int&g...

Ha12312的博客 133

LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)

144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历。 LeetCode144. Binary Tree Preorder Traversal 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭...

weixin_30517001的博客 126

LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++

LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++ Given a binary tree, return thepreordertraversal of its nodes' values. E...

weixin_30472035的博客 88

144. 二叉树的前序遍历(javascript)144. Binary Tree Preorder Traversal

144. 二叉树的前序遍历(javascript)144. Binary Tree Preorder Traversal

ingenuou_的博客 1361

144. Binary Tree Preorder Traversal(二叉树的前序遍历)

给你二叉树的根节点 root ,返回它节点值的 前序 遍历。

apprentice_eye的博客 728

Leetcode144.二叉树前序遍历 Binary Tree Preorder Traversal(Java)

144.Binary Tree Preorder Traversal(Medium) ##Stack##, ##Tree## 题解 二叉树的前序遍历,即遍历顺序为根=>左=>右 递归做法较为简洁 采用非递归做法时要借助栈,对于每个当前结点的右儿子(非空)要最先进栈,因为进栈顺序要取遍历顺序的相反序 **时间复杂度:**O(n) 递归 class Solution { LinkedList<Integer> res = new LinkedList<>();

qq_42411921的博客 264

144. Binary Tree Preorder Traversal (二叉树前序遍历)

Given a binary tree, return thepreordertraversal of its nodes' values. For example:Given binary tree[1,null,2,3], 1 \ 2 / 3 return[1,2,3]. Note:Recu...

anqiu4023的博客 102

算法: 144. 二叉树前序遍历Binary Tree Preorder Traversal

算法: 144. 二叉树前序遍历Binary Tree Preorder Traversal

AI架构师易筋 471

Leetcode-144. Binary Tree Preorder Traversal 二叉树的前序遍历 -python

题目 给定一个二叉树,返回它的 前序 遍历。 链接:https://leetcode.com/problems/binary-tree-preorder-traversal/ Given a binary tree, return the preorder traversal of its nodes’ values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,2,3] 思路及代码 先序遍历 中序遍历 后序遍历 1. DFS Recurs

JamieLuo的博客 346
上一篇: leetcode145. 二叉树的后序遍历 binary-tree-postorder-traversal
下一篇: leetcode143. 重排链表 Reorder List
程序员椰子橙
博客等级 码龄9年 46粉丝 163原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值