当前位置:网站首页>【LeetCode】623. Add a row to the binary tree
【LeetCode】623. Add a row to the binary tree
2022-08-05 08:50: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 <= 105
1 <= depth <= the depth of tree + 1
题解1
思路
- 若为根节点,The new node is directly used as the root node,The root node is returned as the left subtree of the new node
- if not the root node,则遍历到 d e p t h − 1 depth-1 depth−1层,Then insert the new node
代码
# 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时,直接插入新节点,Otherwise recurse its left and right subtrees,直到符合条件
代码
# 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)
边栏推荐
- 行走社会100绝招
- 16 kinds of fragrant rice recipes
- Controlling number and letter input in ASP
- 8.4 Summary of the mock competition
- 六年团队Leader实战秘诀|程序员最重要的八种软技能 - 脸皮薄容易耽误事 - 自我营销
- [Structural Internal Power Cultivation] Structural Realization Stages (2)
- ps怎么把图片变清晰,自学ps软件photoshop2022,简单快速用ps让照片更清晰更有质感
- 【每日一题】1403. 非递增顺序的最小子序列
- 【LeetCode】623. 在二叉树中增加一行
- thinkPHP5 实现点击量(数据自增/自减)
猜你喜欢
代码审计—PHP
[Structural Internal Power Cultivation] The Mystery of Enumeration and Union (3)
Dynamic memory development (C language)
ps怎么把图片变清晰,自学ps软件photoshop2022,简单快速用ps让照片更清晰更有质感
Redis implements distributed lock-principle-detailed explanation of the problem
XSS靶机通关以及XSS介绍
画法几何及工程制图考试卷A卷
SQL语句查询字段内重复内容,并按重复次数加序号
工程制图直线投影练习
TensorFlow安装步骤
随机推荐
控制器-----controller
树状数组模版+例题
全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了
thinkPHP5 实现点击量(数据自增/自减)
Controlling number and letter input in ASP
EA谈单机游戏:仍是产品组合中极其重要的部分
mySQL数据库初始化失败,有谁可以指导一下吗
Ethernet Principle
egg框架中解决跨域的三种方案
请问如果想往mysql里面写数据,直接用flink-connector-jdbc就可以吧,可是我在f
软件系统测试和验收测试有什么联系与区别?专业软件测试方案推荐
ps怎么替换颜色,自学ps软件photoshop2022,ps一张图片的一种颜色全部替换成另外一种颜色
Insights in programming
Adb authorization process analysis
[Structural Internal Power Cultivation] The Mystery of Enumeration and Union (3)
Dynamic memory development (C language)
生命的颜色占卜
RedisTemplate: error template not initialized; call afterPropertiesSet() before using it
手机上流行的各类谜语
Thinking and summary of the efficiency of IT R&D/development process specification