当前位置:网站首页>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;
}
边栏推荐
- 【翻译】混沌网+SkyWalking:为混沌工程提供更好的可观察性
- How can project cost control help project success?
- 电竞、便捷、高效、安全,盘点OriginOS功能的关键词
- Handwriting Currying - toString Comprehension
- SQL外连接之交集、并集、差集查询
- [Unity] [UGUI] [Display text on the screen]
- Can MySQL use aggregate functions without GROUP BY?
- RT-Thread记录(一、RT-Thread 版本、RT-Thread Studio开发环境 及 配合CubeMX开发快速上手)
- Jenkins manual (2) - software configuration
- 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
猜你喜欢
随机推荐
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
MySQL advanced (twenty-seven) database index principle
Common operations of oracle under linux and daily accumulation of knowledge points (functions, timed tasks)
After Keil upgrades to AC6, what changes?
我们的Web3创业项目,黄了
three.js调试工具dat.gui使用
[Unity] [UGUI] [Display text on the screen]
The difference between find, matches, lookingAt matching strings in matcher
Egg framework usage (1)
Four years of weight loss record
MySQL data view
你最隐秘的性格在哪?
Is digital transformation a business buy-in?
第七章,activiti个人任务分配,动态指定和监听器指定任务委派人「建议收藏」
The founder of the DFINITY Foundation talks about the ups and downs of the bear market, and where should DeFi projects go?
第六章:activiti流程分流判断之排它网关和并行网关
用KUSTO查询语句(KQL)在Azure Data Explorer Database上查询LOG实战
SMB + SMB2: Accessing shares return an error after prolonged idle period
皕杰报表的下拉框联动
FPGA: Basic Getting Started Button Controlling LED Lights