当前位置:网站首页>每日一题,二叉树中增加一行
每日一题,二叉树中增加一行
2022-08-10 06:59:00 【小魏爱炸毛】
今天小魏继续为大家带来新的题目
题目如下
给定一个二叉树的根 root 和两个整数 val 和 depth ,在给定的深度 depth 处添加一个值为 val 的节点行。
注意,根节点 root 位于深度 1 。
加法规则如下:
给定整数 depth,对于深度为 depth - 1 的每个非空树节点 cur ,创建两个值为 val 的树节点作为 cur 的左子树根和右子树根。
cur 原来的左子树应该是新的左子树根的左子树。
cur 原来的右子树应该是新的右子树根的右子树。
如果 depth == 1 意味着 depth - 1 根本没有深度,那么创建一个树节点,值 val 作为整个原始树的新根,而原始树就是新根的左子树。
示例 1:
输入: root = [4,2,6,3,1,5], val = 1, depth = 2
输出: [4,1,1,2,null,null,6,3,1,5]
示例 2:
输入: root = [4,2,null,3,1], val = 1, depth = 3
输出: [4,2,null,1,1,3,null,null,1]
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/add-one-row-to-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这个输出结果是二叉树的层序遍历
我们上代码
class Solution {
public TreeNode addOneRow(TreeNode root, int val, int depth) {
if (root == null) {
return null;//当为空树的时候,返回空值
}
if (depth == 1) {
return new TreeNode(val, root, null);//当深度为1时,创建一个新的结点作为新的根节点,而此时原来的根节点就是创建的新的根节点的左子树
}
if (depth == 2) {
root.left = new TreeNode(val, root.left, null);//如果深度为2,那么根据题意在深度为1的地方插入, 插入的val作为原来左子树的根结点,原来的左子树根节点作为新的左子树结点所在的根节点之下左子树的根节点
root.right = new TreeNode(val, null, root.right);
//如果深度为2,那么根据题意在深度为1的地方插入, 插入的val作为原来右子树的根结点,原来右子树根节点作为新的左右子树结点所在的根节点之下右子树的根节点
} else {//深度大于2的时候,我们选择递归的的方式,每次遍历,深度都减1,直到找到深度为2的结点,递归结束,返回root结点
root.left = addOneRow(root.left, val, depth - 1);
root.right = addOneRow(root.right, val, depth - 1);
}
return root;
}
}
以上就是我今天的分享,我们下期再见!
边栏推荐
- 幂函数 指数函数 对数函数
- Sort binary tree code
- 神经网络可视化有3D版本了,美到沦陷 已开源
- Unity3d famous project-Dark Tree translation
- Data types for database learning
- ATH10传感器读取温湿度
- Everyone, the default configuration of oracle cdc occasionally takes 30 seconds to capture data. How to optimize this?
- Big guy, when Oracle single-table incremental synchronization, the source database server takes up nearly 2g of memory. This is not normal, right?
- 2022 Henan Mengxin League No. 5: University of Information Engineering B - Transportation Renovation
- 2022河南萌新联赛第(五)场:信息工程大学 J - AC自动机
猜你喜欢
调试ZYNQ的u-boot 2017.3 不能正常启动,记录调试过程
关于MongoDb查询Decimal128转BigDecimal问题
如何治理资源浪费?百度云原生成本优化最佳实践
1413. Stepwise Summation to Get Minimum Positive Numbers
高级测试:如何使用Flink对Strom任务的逻辑功能进行复现测试?
人工神经网络工作原理,神经网络的工作原理
Nude speech - lying flat - brushing questions - big factory (several tips for Android interviews)
Reproduce dns out-band data combined with sqlmap
时序动作定位 | ASM-Loc:弱监督时序动作定位的动作感知片段建模(CVPR 2022)
初使jest 单元测试
随机推荐
一文2600字手把手教你编写性能测试用例
High quality WordPress download station 5 play theme template
2022 Henan Mengxin League No. 5: University of Information Engineering J-AC Automata
高级测试:如何使用Flink对Strom任务的逻辑功能进行复现测试?
【强化学习】《Easy RL》- Q-learning - CliffWalking(悬崖行走)代码解读
深入理解数组
Text-to-Image最新论文、代码汇总
DGIOT支持工业设备租赁以及远程管控
关于数据库中的中文模糊检索探讨
Grammar Basics (Judgment Statements)
1413. Stepwise Summation to Get Minimum Positive Numbers
PLSQL学习第二天
Grammar Basics (Judgment Statements)
34. Talk about why you want to split the database?What methods are there?
QT下载清华源配置
第2章 变量和基本类型读书笔记
ATH10传感器读取温湿度
30条实用MySQL优化法则
ES13 - ES2022 - The 123rd ECMA Congress approves the ECMAScript 2022 language specification
mysql数据库月增长量问题