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

边栏推荐
- 第七章,activiti个人任务分配,动态指定和监听器指定任务委派人「建议收藏」
- 静态链接和动态链接
- Oracle temporary table space role
- mysql索引
- STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)
- The difference between find, matches, lookingAt matching strings in matcher
- 教你本地编译运行一个IDEA插件,在IDEA里聊天、下棋、斗地主!
- 第五章:redis持久化,包括rdb和aof两种方式[通俗易懂]
- Pycharm 常用外部工具
- QSS 选择器
猜你喜欢

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

Data Middle Office Construction (10): Data Security Management

FPGA: Use of the development environment Vivado

The founder of the DFINITY Foundation talks about the ups and downs of the bear market, and where should DeFi projects go?

RT - Thread record (a, RT, RT Thread version - Thread Studio development environment and cooperate CubeMX quick-and-dirty)

three.js debugging tool dat.gui use

DFINITY 基金会创始人谈熊市沉浮,DeFi 项目该何去何从

数据中台建设(十):数据安全管理

hcip BGP enhancement experiment

华为轻量级神经网络架构GhostNet再升级,GPU上大显身手的G-GhostNet(IJCV22)
随机推荐
数据中台建设(十):数据安全管理
第三章 : redis数据结构种类
Pycharm 常用外部工具
How can project cost control help project success?
The JVM collection that Alibaba's top architects have summarized for many years, where can't I check it!
攻防世界-PWN-new_easypwn
Huawei's lightweight neural network architecture GhostNet has been upgraded again, and G-GhostNet (IJCV22) has shown its talents on the GPU
IO stream articles -- based on io stream to realize folder copy (copy subfolders and files in subfolders) full of dry goods
教你本地编译运行一个IDEA插件,在IDEA里聊天、下棋、斗地主!
基于MindSpore高效完成图像分割,实现Dice!
What is the function of the regular expression replaceAll() method?
Voice-based social software development - making the most of its value
第九章:activit内置用户组设计与组任务分配和IdentityService接口的使用
华为轻量级神经网络架构GhostNet再升级,GPU上大显身手的G-GhostNet(IJCV22)
How to choose coins and determine the corresponding strategy research
First Decentralized Heist?Loss of nearly 200 million US dollars: analysis of the attack on the cross-chain bridge Nomad
浅析WSGI协议
The century-old Nordic luxury home appliance brand ASKO smart wine cabinet in the three-temperature area presents the Chinese Valentine’s Day, and tastes the love of the delicacy
使用工具类把对象中的null值转换为空字符串(集合也可以使用)
高质量 DeFi 应用构建指南,助力开发者玩转 DeFi Summer