102 - Binary Tree Level Order Traversal(DFS)

102. Binary Tree Level Order Traversal [easy] (Python) 题目链接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). For 阅读详情

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

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

Subscribe to see which companies asked this question

思路分析:

1、深度优先遍历算法

#include "stdafx.h"
#include <iostream>
#include <vector>
#include "BinNode.h"

using namespace std;

struct TreeNode
{
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};


class Solution_102_BinaryTreeLevelOrderTraversal
{
private:
	vector<vector<int> > ret;
public:
	void solve(int dep, TreeNode *root)
	{
		if (root == NULL)
		{
			return;
		}


		if (ret.size() > dep)
		{
			ret[dep].push_back(root->val);
		}
		else
		{
			vector<int> a;
			a.push_back(root->val);
			ret.push_back(a);
		}

		solve(dep + 1, root->left);
		solve(dep + 1, root->right);
	}
	vector<vector<int>> levelOrder(TreeNode* root)
	{
		ret.clear();
		solve(0, root);

		return ret;
	}
};

测试用例的构造:

TreeNode *root = new TreeNode(3);

	TreeNode *leftson = new TreeNode(9);
	TreeNode *rightson = new TreeNode(20);
	root->left = leftson;
	root->right = rightson;

	TreeNode *leftson2 = new TreeNode(15);
	TreeNode *rightson2 = new TreeNode(7);
	rightson->left = leftson2;
	rightson->right = rightson2;
	

	Solution_102_BinaryTreeLevelOrderTraversal *s102 = new Solution_102_BinaryTreeLevelOrderTraversal;
	s102->levelOrder(root);


Spyglass功耗分析:从RTL到优化的全流程实践 本文详细介绍了在RTL设计阶段使用Spyglass进行早期功耗分析的全流程实践。通过一个真实项目案例,阐述了早期功耗分析的必要性,并手把手指导了从文件准备、GUI操作到报告解读与优化的完整步骤。文章重点说明了如何利用Spyglass实现功耗分析的“左移”,在编码阶段识别和修复功耗热点,从而避免后期高昂的修改成本,是低功耗芯片设计的关键技能。 阅读详情

相关推荐

Aloudata 荣登“数智技术系列榜单”和“数据智能服务商 TOP30”

持续助力各行业数智化先进企业突破数据管理技术瓶颈,以更加敏捷、精准和可靠的数据交付驱动企业业务创新与经营提效

Aloudata的博客 671

Leetcode 102. Binary Tree Level Order Traversal(BFS+DFS实现)

/** * 可以为每个结点,增加一个变量记录level,方便对结点们按层分别记录 * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x)

Xurui_Luo的博客 210

YOLOv8实战:用Labelme标注自定义数据集并训练分割模型(附完整代码)

本文详细介绍了使用Labelme标注自定义数据集并训练YOLOv8实例分割模型的完整流程。从环境搭建、数据标注与格式转换,到模型训练、调优、验证及部署,提供了实战代码和调优建议。文章还探讨了如何结合追踪目标技术,实现视频流中的实例分割与目标追踪,为计算机视觉应用提供了一套可复制的解决方案。

552

汽车电子电气架构

控制器:从多独立 ECU → 域控制器(DCU) → 中央计算平台 + 区域控制器,硬件集成度持续提升,冗余减少;算力:从分散无集中算力 → 按域分配算力 → 全域算力集中,算力量级从 MIPS 跃升至 TOPS,支撑高阶智驾;网络:从低速 CAN/LIN 独占 → 以太网为主、总线为辅 → 高速以太网主干,数据传输速率提升千倍;软硬件:从软硬件深度绑定 → 初步解耦 → 完全解耦(SOA),实现 “硬件预埋、软件解锁”;线束。

LXY_LXY_的博客 1439

1 FFmpeg从入门到精通-FFmpeg简介

1.FFmpeg简介 1.1 FFmpeg的定义 1.2 FFmpeg的历史 1.3 FFmpeg的基本组成 1.4 FFmpeg的编解码工具ffmpeg 1.5 FFmpeg的播放器ffplay 1.6 FFmpeg的多媒体分析器ffprobe 1.7 FFmpeg编译(1)FFmpeg之Windows平台编译(2)FFmpeg之Linux平台编译(3)FFmpeg之OS X平台编译 1.8 FFmpeg编码支持与定制(1)FFmpeg的编码器支持(2)FFmpe

migu123的专栏 3698

LintCode / LeetCode 102. Binary Tree Level Order Traversal | Queue or Stack DFS | C++ Python

解法一 queue class Solution { public: vector&lt;vector&lt;int&gt;&gt; levelOrder(TreeNode * root) { queue&lt;TreeNode*&gt; q; vector&lt;vector&lt;int&gt;&gt; res; if(!root) ...

real_lisa的博客 149

[UVM]UVM TLM2.0简介及应用实例分析

本文介绍了TLM2.0在UVM中的实现与应用。TLM2.0作为SystemC模型间的核心传输机制,相比TLM1.0提供了更丰富的特性,包括双向阻塞/非阻塞接口、时间标记和统一数据包。文章详细解析了TLM2.0在UVM中的接口实现方式,包括socket类型定义、数据传输机制以及时间标记处理。通过一个具体实例,展示了如何在UVM组件中使用TLM2.0的initiator和target socket进行数据传输,并说明了标准数据包格式和精确时间标记的优势。最后指出TLM2.0在UVM中的实现主要是为了与Syste

元直的博客 5398

LeetCode102. Binary Tree Level Order Traversal(C++)

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 ...

qq_41562704的博客 292

166、电流环带宽测试

本文分享了电流环带宽测试的实战经验,揭示了常见误区:误将系统其他环节(如PWM、ADC、传感器)的延迟当作电流环性能。强调带宽测试需先断开速度/位置环,采用扫频法注入正弦信号,结合伯德图分析幅值与相位响应。关键点包括:控制频率步进为对数、确保采样时序精准、避免传感器带宽限制影响结果。文中指出“假带宽”现象(如鼓包、增益超0dB)多源于非线性或机械共振,建议遵循“先开环后闭环”“带宽非越高越好”“阶跃验证”三原则,并建立出厂基准曲线以支持后期故障诊断。

2401_88858637的博客 178

基于DeepFashion2数据集的服装检测系统:YOLOv11实现与UI界面开发

80万张高质量服装图片13个服装类别:短袖上衣、长袖上衣、短袖外套、长袖外套、背心、短裤、长裤、短裙、长裙、袜子、帽子、鞋、包包每张图片包含边界框、分割掩码、landmarks等标注多样化的场景:商业图片、街拍图片、消费者图片更高效的Backbone网络改进的特征金字塔网络(FPN)自适应训练样本分配策略更精确的损失函数以下是YOLOv11的模型定义:python复制下载# 模型参数self.nc = nc # 类别数。

YOLO 455

[LeetCode 107]Binary Tree Level Order Traversal II(递归法)

题目内容107 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 exa

yangjin95的博客 916

通信算法之283:大疆DJI云哨系统-DJI DroneID物理层协议-解速率匹配及Turbo译码

3. 完整云哨接收机,接收灵敏度。交流: 1279682290 ,2. Turbo译码模块。1. 解速率匹配模块。

leegang12的专栏 868

102. 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). For example: Given binary tree [3,9,20,null,null,15,7], 3 /

记录点滴 321

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

leetcode---102. Binary Tree Level Order Traversaleode C++ 题解

102. Binary Tree Level Order Traversal C++,应该是最短的代码了。 层序遍历一般来说确实是用队列实现的,但是这里很明显用递归前序遍历就能实现呀,而且复杂度O(n)。。。 要点有几个: 利用depth变量记录当前在第几层(从0开始),进入下层时depth + 1; 如果depth &gt;= vector.size()说明这一层还没来过,这是第一次来,...

bbkgl的博客 372

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

LeetCode 102. Binary Tree Level Order Traversal 题解

本题是二叉树遍历的经典问题,主要考察对广度优先搜索和深度优先搜索的理解和应用。广度优先搜索:层次清晰,直接按照层次顺序处理节点,代码逻辑直观。深度优先搜索:通过递归实现,代码简洁,但需要额外记录深度信息。在实际应用中,广度优先搜索通常是解决层序遍历问题的首选方法,因为它天然适合按照层次顺序处理节点。而深度优先搜索则在其他类型的树遍历问题中更为常用。无论是哪种方法,掌握二叉树的遍历技巧对于解决树相关的算法问题都非常重要。

cannonjinx的博客 4510

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

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

Leetcode102. Binary Tree Level Order Traversal

Leetcode102. 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). For example: Given binary tree [3...

magic_jiayu的博客 282

102. Binary Tree Level Order Traversal

题目描述(中等难度) 二叉树的层次遍历,输出一个 list 的 list。 解法一 DFS 这道题考的就是 BFS,我们可以通过 DFS 实现。只需要在递归过程中将当前 level 传入即可。 import java.util.ArrayList; import java.util.List; class TreeNode{ int val; TreeNode left; TreeNode right; TreeNode(int x){val=x;} } public class Binary

猫敷雪 252

python --LeetCode之102. 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). 题目理解: 可以使用广度优先搜索和深度优先搜索。 方法一:广度优先搜索。 利用队列,将每层的节点先存入队列,然后一个个取出来遍历每个节点的左右节点继续存入,...

不服输的南瓜的博客 312

【LeetCode每日打卡】102. 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). For example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its level order traversal as: [ ...

韩旭051的博客 577

LeetCode 102. Binary Tree Level Order Traversal & LeetCode 107. Binary Tree Level Order Traversal II

LeetCode 102. Binary Tree Level Order Traversal (Medium) && LeetCode 107. Binary Tree Level Order Traversal II (Easy) 主要知识点:树的层次遍历、前序遍历;优先级:4

温迪今天刷题了吗 227

LeetCode - 102. Binary Tree Level Order Traversal

题目 LeetCode - 102. Binary Tree Level Order Traversal 题目链接 https://leetcode.com/problems/binary-tree-level-order-traversal/ 解题思路 本题采取简单思路,先求树的最大深度是为确定适合的ans大小,然后常规的深度优先遍历。 解题源码 /** * Definition for a ...

virgilshi的博客 171

LeetCode笔记:102. Binary Tree Level Order Traversal

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

Minecraft 1020

LeetCode 102. Binary Tree Level Order Traversal

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). For example: Given binary tree [3,9,20,null,nul...

ZQYnn's blog 140
上一篇: 70 - Climbing Stairs
下一篇: 102 - Binary Tree Level Order Traversal(BFS)
hellochenlu
博客等级 码龄14年 97粉丝 184原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值