当前位置:网站首页>623. Add a row to a binary tree: Simple binary tree traversal problems
623. Add a row to a binary tree: Simple binary tree traversal problems
2022-08-05 11:38:00 【Mitsuha Miyamizu's writing diary】
题目描述
这是 LeetCode 上的 623. 在二叉树中增加一行 ,难度为 中等.
Tag : 「二叉树」、「BFS」、「DFS」
给定一个二叉树的根 root
和两个整数 val
和 depth
,在给定的深度 depth
处添加一个值为 val
的节点行.
注意,根节点 root
位于深度 .
加法规则如下:
给定整数 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]
提示:
节点数在 范围内 树的深度在 范围内
BFS
根据 BFS
来做,每次 BFS
The entire layer expand,At the same time record the current depth,当到达第 depth - 1
层,Is to add some operation.
Java 代码:
class Solution {
public TreeNode addOneRow(TreeNode root, int val, int depth) {
if (depth == 1) {
TreeNode ans = new TreeNode(val);
ans.left = root;
return ans;
}
Deque<TreeNode> d = new ArrayDeque<>();
d.addLast(root);
int cur = 1;
while (!d.isEmpty()) {
int sz = d.size();
while (sz-- > 0) {
TreeNode t = d.pollFirst();
if (cur == depth - 1) {
TreeNode a = new TreeNode(val), b = new TreeNode(val);
a.left = t.left; b.right = t.right;
t.left = a; t.right = b;
} else {
if (t.left != null) d.addLast(t.left);
if (t.right != null) d.addLast(t.right);
}
}
cur++;
}
return root;
}
}
TypeScript 代码:
function addOneRow(root: TreeNode | null, val: number, depth: number): TreeNode | null {
if (depth == 1) {
const ans = new TreeNode(val)
ans.left = root
return ans
}
const stk = new Array<TreeNode>()
let he = 0, ta = 0, cur = 1
stk[ta++] = root
while (he < ta) {
let sz = ta - he
while (sz-- > 0) {
const t = stk[he++]
if (cur == depth - 1) {
const a = new TreeNode(val), b = new TreeNode(val)
a.left = t.left; b.right = t.right
t.left = a; t.right = b
} else {
if (t.left != null) stk[ta++] = t.left
if (t.right != null) stk[ta++] = t.right
}
}
cur++
}
return root
};
时间复杂度: 空间复杂度:
DFS
同理,使用 DFS
Also can be carried out to solve,在 DFS
In the process of recording the current depth.
Java 代码:
class Solution {
int d, v;
public TreeNode addOneRow(TreeNode root, int val, int depth) {
d = depth; v = val;
if (d == 1) {
TreeNode ans = new TreeNode(val);
ans.left = root;
return ans;
}
dfs(root, 1);
return root;
}
void dfs(TreeNode root, int cur) {
if (root == null) return ;
if (cur == d - 1) {
TreeNode a = new TreeNode(v), b = new TreeNode(v);
a.left = root.left; b.right = root.right;
root.left = a; root.right = b;
} else {
dfs(root.left, cur + 1);
dfs(root.right, cur + 1);
}
}
}
TypeScript 代码:
let d = 0, v = 0
function addOneRow(root: TreeNode | null, val: number, depth: number): TreeNode | null {
d = depth; v = val
if (d == 1) {
const ans = new TreeNode(v)
ans.left = root
return ans
}
dfs(root, 1)
return root
};
function dfs(root: TreeNode | null, cur: number): void {
if (root == null) return
if (cur == d - 1) {
const a = new TreeNode(v), b = new TreeNode(v)
a.left = root.left; b.right = root.right
root.left = a; root.right = b
} else {
dfs(root.left, cur + 1)
dfs(root.right, cur + 1)
}
}
时间复杂度: 空间复杂度:
最后
这是我们「刷穿 LeetCode」系列文章的第 No.623
篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完.
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码.如果涉及通解还会相应的代码模板.
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode .
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解.
更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地
边栏推荐
- 5G NR system messages
- 灰度值与热成像理解
- 智源社区AI周刊No.92:“计算复杂度”理论奠基人Juris Hartmanis逝世;美国AI学生九年涨2倍,大学教师短缺;2022智源大会观点报告发布[附下载]
- 【心里效应】98 个著名的心理效应
- 2022杭电杯超级联赛(5)
- 华为分析&联运活动,助您提升游戏总体付费
- STM32 entry development: write XPT2046 resistive touch screen driver (analog SPI)
- Student Information Management System (first time...)
- Apache APISIX Ingress v1.5-rc1 发布
- 脱光衣服待着就能减肥,当真有这好事?
猜你喜欢
随机推荐
祝所有码农七夕快乐~
ECCV 2022 | 视听分割:全新任务,助力视听场景像素级精细化理解
“小钢炮”气质明显,安全、舒适一个不落
hdu 1870 愚人节的礼物 (栈)
普通二本毕业八年,京东就职两年、百度三年,分享大厂心得
PG优化篇--执行计划相关项
knife4j
PostgreSQL 2022 报告:流行度上涨,开源、可靠性和扩展是关键
动手学深度学习_GoogLeNet / Inceptionv1v2v3v4
.NET in-depth analysis of the LINQ framework (6: LINQ execution expressions)
导火索:OAuth 2.0四种授权登录方式必读
Machine Learning - Logistic Regression
5G NR 系统消息
我要抓狂了。。又回到了几天不能A一道题的时候
智源社区AI周刊No.92:“计算复杂度”理论奠基人Juris Hartmanis逝世;美国AI学生九年涨2倍,大学教师短缺;2022智源大会观点报告发布[附下载]
nyoj1185最大最小值(线段树)
MySQL 中 auto_increment 自动插入主键值
Linux: Remember to install MySQL8 on CentOS7 (blog collection)
官方发布·2022南京智博会定于10月份在新庄国展召开
自定义过滤器和拦截器实现ThreadLocal线程封闭