当前位置:网站首页>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流程分流判断之排它网关和并行网关
- IO stream articles -- based on io stream to realize folder copy (copy subfolders and files in subfolders) full of dry goods
- Complete image segmentation efficiently based on MindSpore and realize Dice!
- Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
- [Unity] [UGUI] [Display text on the screen]
- Microcontroller: temperature control DS18B20
- Offensive World-PWN-new_easypwn
- uniapp 连接ibeacon
- FPGA:基础入门按键控制LED灯
- 第三章 : redis数据结构种类
猜你喜欢

百年北欧奢华家电品牌ASKO智能三温区酒柜臻献七夕,共品珍馐爱意

Microcontroller: temperature control DS18B20

Pycharm 常用外部工具

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

产品太多了,如何实现一次登录多产品互通?

Jenkins使用手册(2) —— 软件配置

leetcode: 529. Minesweeper Game

Complete image segmentation efficiently based on MindSpore and realize Dice!

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

阿里全新推出:微服务突击手册,把所有操作都写出来了PDF
随机推荐
第七章,activiti个人任务分配,动态指定和监听器指定任务委派人「建议收藏」
linux下oracle常见操作以及日常积累知识点(函数、定时任务)
第九章:activit内置用户组设计与组任务分配和IdentityService接口的使用
19. Server-side session technology Session
导火索:OAuth 2.0四种授权登录方式必读
Where is your most secretive personality?
第三章 : redis数据结构种类
NowCoderTOP35-40 - continuous update ing
攻防世界-PWN-new_easypwn
第五章:redis持久化,包括rdb和aof两种方式[通俗易懂]
5. Deploy the web project to the cloud server
这份阿里强推的并发编程知识点笔记,将是你拿大厂offer的突破口
Voice-based social software development - making the most of its value
Technical dry goods | Hausdorff distance for image segmentation based on MindSpore
js劫持数组push方法
NowCoderTOP35-40——持续更新ing
还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!
Development common manual link sharing
首次去中心化抢劫?近2亿美元损失:跨链桥Nomad 被攻击事件分析
阿里全新推出:微服务突击手册,把所有操作都写出来了PDF