当前位置:网站首页>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;
}
边栏推荐
- 上位机开发C#语言:模拟STC串口助手接收单片机发送数据
- Jenkins manual (2) - software configuration
- 第五章:redis持久化,包括rdb和aof两种方式[通俗易懂]
- 创建一个 Dapp,为什么要选择波卡?
- Common operations of oracle under linux and daily accumulation of knowledge points (functions, timed tasks)
- Imitation SBUS fixed with serial data conversion
- 基于MindSpore高效完成图像分割,实现Dice!
- 【Office】Microsoft Office下载地址合集(微软官方原版离线安装下载)
- SD NAND Flash简介!
- RT - Thread record (a, RT, RT Thread version - Thread Studio development environment and cooperate CubeMX quick-and-dirty)
猜你喜欢
Create a Dapp, why choose Polkadot?
产品太多了,如何实现一次登录多产品互通?
RT - Thread record (a, RT, RT Thread version - Thread Studio development environment and cooperate CubeMX quick-and-dirty)
【温度预警程序de开发】事件驱动模型实例运用
PCB layout must know: teach you to correctly lay out the circuit board of the op amp
What are the standards for electrical engineering
Confessing in the era of digital transformation: Mai Cong Software allows enterprises to use data in the easiest way
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
Egg framework usage (2)
Technical dry goods | Hausdorff distance for image segmentation based on MindSpore
随机推荐
Jenkins manual (2) - software configuration
用KUSTO查询语句(KQL)在Azure Data Explorer Database上查询LOG实战
电气工程的标准是什么
NowCoderTOP35-40 - continuous update ing
静态链接和动态链接
STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)
第六章:activiti流程分流判断之排它网关和并行网关
【AGC】增长服务1-远程配置示例
Analysis and practice of antjian webshell dynamic encrypted connection
告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来
Brief Analysis of WSGI Protocol
[Unity] [UGUI] [Display text on the screen]
阿里全新推出:微服务突击手册,把所有操作都写出来了PDF
The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
Microservice Technology Stack
牛刀小试基本语法,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang基本语法和变量的使用EP02
使用工具类把对象中的null值转换为空字符串(集合也可以使用)
uniapp 连接ibeacon
数分面试(一)----与业务相关
The JVM collection that Alibaba's top architects have summarized for many years, where can't I check it!