当前位置:网站首页>Leetcode刷题——623. 在二叉树中增加一行
Leetcode刷题——623. 在二叉树中增加一行
2022-08-05 10:19:00 【lonelyMangoo】
是今天的每日一题,题目地址:623. 在二叉树中增加一行
思路
看到对每一层进行操作,首先就是想到层次遍历。当现在的深度等于给定的depth-1时记录当前节点(可以直接用queue,当时没想到)开始操作,操作时要注意区分注意要区分孩子是否为空的情况:
孩子为空,直接添加;孩子不为空,相当于加一层,添到后面去。
代码:
public TreeNode addOneRow(TreeNode root, int val, int depth) {
if (depth == 1) {
TreeNode node = new TreeNode(val);
node.left = root;
return node;
}
Queue<TreeNode> queue = new LinkedList<>();
if(root==null){
return new TreeNode(val);
}
queue.offer(root);
int nowDepth = 0;
while (!queue.isEmpty()) {
nowDepth++;
int len = queue.size() - 1;
List<TreeNode> list = new ArrayList<>();
while (len-- >= 0) {
TreeNode poll = queue.poll();
if(nowDepth == depth - 1){
list.add(poll);
}
if (poll.left != null) {
queue.offer(poll.left);
}
if (poll.right != null) {
queue.offer(poll.right);
}
}
// 目标
if(nowDepth==depth-1){
//最后一行
for (TreeNode node : list) {
if(node.left!=null){
TreeNode newNode = new TreeNode(val);
newNode.left=node.left;
node.left=newNode;
}
else {
node.left=new TreeNode(val);
}
if(node.right!=null){
TreeNode newNode = new TreeNode(val);
newNode.right=node.right;
node.right=newNode;
}
else {
node.right=new TreeNode(val);
}
}
break;
}
}
return root;
}
改进
之所以代码这么冗余是因为我忽略了TreeNode有另一种构造方法,可以直接声明左子树和右子树,就避免了很多冗余的操作。
public TreeNode addOneRow(TreeNode root, int val, int depth) {
if (depth == 1) {
TreeNode node = new TreeNode(val);
node.left = root;
return node;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int newDepth = 0;
while (!queue.isEmpty()){
newDepth++;
int len = queue.size()-1;
if(newDepth == depth-1){
while (!queue.isEmpty()){
TreeNode poll = queue.poll();
poll.left =new TreeNode(val,poll.left,null);
poll.right =new TreeNode(val,null,poll.right);
}
break;
}else {
while (len-- >= 0) {
TreeNode peek = queue.poll();
if (peek.left != null) queue.offer(peek.left);
if (peek.right != null) queue.offer(peek.right);
}
}
}
return root;
}
边栏推荐
- 用户考试分数大于单科科目平均分的查询
- 一文道清什么是SPL
- 2022 Huashu Cup Mathematical Modeling Ideas Analysis and Exchange
- PHP 操作mangoDb
- Which big guy has the 11G GI and ojvm patches in April or January 2020, please help?
- 【综合类型第 35 篇】程序员的七夕浪漫时刻
- MySQL之数据视图
- STM32+ULN2003驱动28BYJ4步进电机(根据圈数正转、反转)
- 这份阿里强推的并发编程知识点笔记,将是你拿大厂offer的突破口
- three objects are arranged in a spherical shape around the circumference
猜你喜欢
首次去中心化抢劫?近2亿美元损失:跨链桥Nomad 被攻击事件分析
【MindSpore Easy-Diantong Robot-01】You may have seen many knowledge quiz robots, but this one is a bit different
linux下oracle常见操作以及日常积累知识点(函数、定时任务)
Microcontroller: temperature control DS18B20
This notebook of concurrent programming knowledge points strongly recommended by Ali will be a breakthrough for you to get an offer from a big factory
RT-Thread记录(一、RT-Thread 版本、RT-Thread Studio开发环境 及 配合CubeMX开发快速上手)
Oracle temporary table space role
如何选币与确定对应策略研究
[Strong Net Cup 2022] WP-UM
还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!
随机推荐
第九章:activit内置用户组设计与组任务分配和IdentityService接口的使用
多线程(进阶) - 2.5w字总结
Confessing in the era of digital transformation: Mai Cong Software allows enterprises to use data in the easiest way
What is SPL?
第五章:activiti流程分流判断,判断走不同的任务节点
19.3 restart the Oracle environment
2022华数杯数学建模思路分析交流
Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
牛刀小试基本语法,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang基本语法和变量的使用EP02
SQL外连接之交集、并集、差集查询
第四章:redis 数组结构的set和一些通用命令「建议收藏」
uniapp connect ibeacon
E-sports, convenience, efficiency, security, key words for OriginOS functions
Voice-based social software development - making the most of its value
登录功能和退出功能(瑞吉外卖)
华为轻量级神经网络架构GhostNet再升级,GPU上大显身手的G-GhostNet(IJCV22)
【温度预警程序de开发】事件驱动模型实例运用
教你本地编译运行一个IDEA插件,在IDEA里聊天、下棋、斗地主!
Analysis and practice of antjian webshell dynamic encrypted connection
STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)