当前位置:网站首页>leetcode-每日一题623. 在二叉树中增加一行(DFS)
leetcode-每日一题623. 在二叉树中增加一行(DFS)
2022-08-05 12:43:00 【lin钟一】

题目链接:https://leetcode.cn/problems/add-one-row-to-tree/
思路
方法一、DFS
直接想法
这题的要求:根节点为深度1开始,在depth深度创建一个新的节点,把原来depth深度的节点,父节点的左子节点依旧为新节点的左子节点,右子节点依旧为新节点的右子节点。
这个时候我们可以用DFS从根节点开始遍历节点的左右节点,一直找到depth深度的节点,创建新的节点替换他,然后返回给父节点新的节点,对于depth深度存在问题,这里我们需要分类讨论
- depth深度为1:需要在根节点创建新的节点,去替代根节点
- depth深度在(1,n):这里的n是树的深度,就是在树中间插入节点,用DFS遍历递归到相应节点进行替换
- depth深度在n:在树的叶子节点出插入新节点,不需要替换,只需要创建新的节点即可
代码示例
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */
func addOneRow(root *TreeNode, val int, depth int) *TreeNode {
var dfs func(root *TreeNode, depth int, flag int) *TreeNode
dfs = func(root *TreeNode, depth int, flag int) *TreeNode{
if depth == 1{
var node *TreeNode
if flag == 1{
node = &TreeNode{
Val: val,
Left: root,
Right: nil,
}
}else{
node = &TreeNode{
Val: val,
Left: nil,
Right: root,
}
}
return node
}
if root == nil{
return nil
}
root.Left = dfs(root.Left, depth - 1, 1)
root.Right = dfs(root.Right, depth - 1, 0)
return root
}
return dfs(root, depth, 1)
}

复杂度分析
- 时间复杂度:O(2depth - 1),表示遍历到深度为depth需要访问的节点数,这里计算的是在depth深度是满二叉树的情况下
- 空间复杂度:O(2depth-1),表示在深度为depth需要创建的新的节点数为2depth-1个新TreeNode
边栏推荐
- 2022华数杯C:插层熔喷非织造材料的性能控制研究
- TypeScript的崛起之路给我们带来的选谷思路
- Translation of "Grandmaster level in StarCraft II using multi-agent reinforcement learning"
- RK3568+Hongmeng Industrial Flat Panel Industrial Kanban Solution Design
- 通俗易懂玩QT:QT程序发布打包
- Jiang: in the second half of 2022 the 22 rule to buck the trend
- 可编程直流电源是一种稳定可靠的电源设备
- Regular expressions in action
- How does the bank transaction system ensure strong consistency of data transactions?Via the database component?How to ensure the normal consistency of database transaction data under high concurrency?
- The fourth SQL general command (DML)
猜你喜欢

比较方法equals( )、==以及CompareTo

我和TiDB的故事 | 遇上你是我的缘

浅解排列与组合

CC2530实现按键中断

RK3588+FPGA高速图像处理通信处理机解决方案

Jiang: in the second half of 2022 the 22 rule to buck the trend

Query optimization (TTFB is too long) left join index does not take effect

阿里二面:明明加了唯一索引,为什么还是产生重复数据?

RT-Thread recording (2. RT-Thread kernel startup process - startup file and source code analysis)

二:OpenCV图片叠加逻辑运算
随机推荐
Bit rate vs. resolution, which one is more important?
Oracle Database 19c 的10大新特性一览
可编程直流电源是一种稳定可靠的电源设备
【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例...
Mysql索引
在线问题反馈模块实战(十九):实现数据批量导出到excel文件中功能
express logging module Morgan
hello world, hello Keke people
Should you migrate your monolithic architecture to microservices?
2022.08.03_Daily Question
MySQL之InnoDB线程模型
我和 TiDB 的故事 | 横看成岭侧成峰
Small household appliance industry supply chain collaborative management system: help enterprises break through market competition and strengthen the rapid response capability of the supply chain
WPF开发随笔收录-WriteableBitmap绘制高性能曲线图
163_Tricks_Power BI one-click batch creation of custom field parameters
C进阶 - 指针进阶
Grid Infrastructure Installation Fails with Error
对话庄表伟:开源第一课
COSCon'22城市/学校/机构出品人征集令
试写C语言扫雷