当前位置:网站首页>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;
}

边栏推荐
- 2022杭电多校 第6场 1008.Shinobu Loves Segment Tree 规律题
- 创建一个 Dapp,为什么要选择波卡?
- Common operations of oracle under linux and daily accumulation of knowledge points (functions, timed tasks)
- Egg framework usage (1)
- mysql索引
- three objects are arranged in a spherical shape around the circumference
- JS逆向入门学习之回收商网,手机号码简易加密解析
- Huawei's lightweight neural network architecture GhostNet has been upgraded again, and G-GhostNet (IJCV22) has shown its talents on the GPU
- [Office] Collection of Microsoft Office download addresses (offline installation and download of Microsoft's official original version)
- Microcontroller: temperature control DS18B20
猜你喜欢

SQL Outer Join Intersection, Union, Difference Query

STM32+ULN2003驱动28BYJ4步进电机(根据圈数正转、反转)

Egg framework usage (1)
![[强网杯2022]WP-UM](/img/3d/caeab05ddca278af274dbf6e2f8ba1.png)
[强网杯2022]WP-UM

Voice-based social software development - making the most of its value

leetcode: 529. Minesweeper Game

three物体围绕一周呈球形排列

JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number

linux下oracle常见操作以及日常积累知识点(函数、定时任务)

High-quality DeFi application building guide to help developers enjoy DeFi Summer
随机推荐
机器学习-基础知识 - Precision, Recall, Sensitivity, Specificity, Accuracy, FNR, FPR, TPR, TNR, F1 Score, Bal
第七章,activiti个人任务分配,动态指定和监听器指定任务委派人「建议收藏」
Microservice Technology Stack
three objects are arranged in a spherical shape around the circumference
Analysis and practice of antjian webshell dynamic encrypted connection
Microcontroller: temperature control DS18B20
JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number
深入理解 Istio 流量管理的超时时间设置
IDEA performs the Test operation, resulting in duplicate data when data is inserted
three物体围绕一周呈球形排列
上位机开发C#语言:模拟STC串口助手接收单片机发送数据
我们的Web3创业项目,黄了
阿里顶级架构师多年总结的JVM宝典,哪里不会查哪里!
导火索:OAuth 2.0四种授权登录方式必读
攻防世界-PWN-new_easypwn
Handwriting Currying - toString Comprehension
SQL外连接之交集、并集、差集查询
Development common manual link sharing
你最隐秘的性格在哪?
three.js调试工具dat.gui使用