当前位置:网站首页>【LeetCode】623. 在二叉树中增加一行
【LeetCode】623. 在二叉树中增加一行
2022-08-05 08:37:00 【pass night】
题目
给定一个二叉树的根 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]
提示:
- 节点数在
[1, 104]范围内 - 树的深度在
[1, 104]范围内 -100 <= Node.val <= 100-105 <= val <= 1051 <= depth <= the depth of tree + 1
题解1
思路
- 若为根节点,则直接将新节点作为根节点,根节点作为新节点的左子树返回
- 若非根节点,则遍历到 d e p t h − 1 depth-1 depth−1层,然后将新节点插入
代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if depth == 1:
node = TreeNode(val)
node.left = root
root = node
return root
queue = collections.deque([root])
while queue and depth > 2:
depth -= 1
for _ in range(len(queue)):
cur = queue.popleft()
if cur.left: queue.append(cur.left)
if cur.right: queue.append(cur.right)
for _ in range(len(queue)):
cur = queue.popleft()
if cur.left:
node = TreeNode(val)
node.left = cur.left
cur.left = node
else:
cur.left = TreeNode(val)
if cur.right:
node = TreeNode(val)
node.right = cur.right
cur.right = node
else:
cur.right = TreeNode(val)
return root
复杂度
- 时间复杂度: O ( n ) O(n) O(n)
- 空间复杂度: O ( n ) O(n) O(n)
题解2
思路
- 当 d e p t h ∈ 1 , 2 depth \in {1,2} depth∈1,2时,直接插入新节点,否则递归其左右子树,直到符合条件
代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if not root: return None
if depth == 1:
return TreeNode(val,root,None)
if depth == 2:
root.left = TreeNode(val,root.left, None)
root.right = TreeNode(val, None, root.right)
else:
root.left = self.addOneRow(root.left, val, depth-1)
root.right = self.addOneRow(root.right, val, depth-1)
return root
复杂度
- 时间复杂度: O ( n ) O(n) O(n)
- 空间复杂度: O ( n ) O(n) O(n)
边栏推荐
- 达梦数据库大表添加字段
- Redis implements distributed lock-principle-detailed explanation of the problem
- Constellation ideal lover
- How to make a puzzle in PS, self-study PS software photoshop2022, PS make a puzzle effect
- mySQL数据库初始化失败,有谁可以指导一下吗
- 在ASP控制数字及字母输入
- CROS and JSONP configuration
- Basic introduction of stack and queue and C language implementation of functions such as creation, destruction, entry and exit, counting the number of elements, viewing elements, etc., as well as stac
- Redis实现分布式锁-原理-问题详解
- 网络安全研究发现,P2E项目遭遇黑客攻击只是时间问题
猜你喜欢

EA谈单机游戏:仍是产品组合中极其重要的部分
![[Structural Internal Power Cultivation] The Mystery of Enumeration and Union (3)](/img/39/d20f45ccc86ebc4e5aebc8e4d0115f.png)
[Structural Internal Power Cultivation] The Mystery of Enumeration and Union (3)

【结构体内功修炼】枚举和联合的奥秘(三)

Data source object management Druid and c3p0

Adb 授权过程分析

Ethernet Principle

MySQL 数据库 报错 The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)

Ethernet Principle

吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)

Pagoda measurement - building small and medium-sized homestay hotel management source code
随机推荐
The color of life divine
love is a sad song
彩绘漂亮MM集
Stored procedure writing experience and optimization measures
DPU — 功能特性 — 网络系统的硬件卸载
Three solutions to solve cross-domain in egg framework
Nn. Unfold and nn. The fold
ps怎么把图片变清晰,自学ps软件photoshop2022,简单快速用ps让照片更清晰更有质感
JS syntax usage
php fails to write data to mysql
吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)
[转帖]嫁人一定要嫁工资至少比你高3571.4元的男士
Redis cache and existing problems--cache penetration, cache avalanche, cache breakdown and solutions
画法几何及工程制图考试卷A卷
漂亮MM和普通MM的区别
Codeforce 8.1-8.7做题记录
The magic weapon for small entrepreneurs!
长期招聘嵌入式开发-深圳宝安
哪个是你爱情的颜色?
【 a daily topic 】 1403. The increasing order of the sequence, boy