623. Add One Row to Tree
Problem statement
Given the root of a binary tree, then value
vand depthd, you need to add a row of nodes with valuevat the given depthd. The root node is at depth 1.The adding rule is: given a positive integer depth
d, for each NOT null tree nodesNin depthd-1, create two tree nodes with valuevasN'sleft subtree root and right subtree root. AndN'soriginal left subtree should be the left subtree of the new left subtree root, its original right subtree should be the right subtree of the new right subtree root. If depthdis 1 that means there is no depth d-1 at all, then create a tree node with value v as the new root of the whole original tree, and the original tree is the new root's left subtree.Example 1:
Input:
A binary tree as following:
4
/ \
2 6
/ \ /
3 1 5 v = 1 d = 2 Output:
4
/ \
1 1
/ \
2 6
/ \ /
3 1 5Example 2:
Input:
A binary tree as following:
4
/
2
/ \
3 1 v = 1 d = 3 Output:
4
/
2
/ \
1 1
/ \
3 1Note:
- The given d is in range [1, maximum depth of the given tree + 1].
- The given binary tree has at least one tree node.
Solution
This is the second problem of leetcode weekly contest 37, which is involved with a binary tree. According to what described, obviously, there are two solutions: BFS and DFS.
BFS
This is the BFS problem for a tree, normally, we should get the size of the queue before we process each level, which is different with the BFS in two dimension matrix.
Time complexity is O(n), space complexity is O(n).
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* addOneRow(TreeNode* root, int v, int d) {
if(d == ){
TreeNode* node = new TreeNode(v);
node->left = root;
return node;
}
queue<TreeNode*> que;
que.push(root);
while(!que.empty() && d > ){
d--;
int size = que.size();
while(size > ){
TreeNode* node = que.front();
que.pop();
if(d == ){
TreeNode* left_node = new TreeNode(v);
left_node->left = node->left;
node->left = left_node;
TreeNode* right_node = new TreeNode(v);
right_node->right = node->right;
node->right = right_node;
} else {
if(node->left){
que.push(node->left);
}
if(node->right){
que.push(node->right);
}
}
size--;
}
}
return root;
}
};
DFS
This is the best solution for this problem, it is
More accurate, this is a divide and conquer problem. We conquer first in current level and divide to lower level.
Time complexity is O(n), space complexity is O(1).
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* addOneRow(TreeNode* root, int v, int d) {
// at least one root node, no need do root == NULL test
if(d == ){
TreeNode* dummy = new TreeNode(v);
dummy->left = root;
return dummy;
}
add_node(root, v, d - );
return root;
}
private:
void add_node(TreeNode* node, int val, int depth){
// conquer
if(node == NULL){
return;
}
if(depth == ){
TreeNode* left_node = new TreeNode(val);
left_node->left = node->left;
node->left = left_node;
TreeNode* right_node = new TreeNode(val);
right_node->right = node->right;
node->right = right_node;
return;
}
// divide
add_node(node->left, val, depth - );
add_node(node->right, val, depth - );
return;
}
};
623. Add One Row to Tree的更多相关文章
- 【LeetCode】623. Add One Row to Tree 解题报告(Python)
[LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...
- [LeetCode] 623. Add One Row to Tree 二叉树中增加一行
Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value ...
- 【leetcode】623. Add One Row to Tree
题目如下: Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with ...
- [LeetCode] Add One Row to Tree 二叉树中增加一行
Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value ...
- [Swift]LeetCode623. 在二叉树中增加一行 | Add One Row to Tree
Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value ...
- [leetcode-623-Add One Row to Tree]
Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value ...
- 【跟着stackoverflow学Pandas】add one row in a pandas.DataFrame -DataFrame添加行
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- add new row to data.frame/dataframe
df<-NULL new_row<-data.frame(colA="xxx",colB=123) df<-rbind(df,new_row)
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- 非常实用的Linux 系统监控工具
随着互联网行业的不断发展,各种监控工具多得不可胜数.这里列出网上最全的监控工具.让你可以拥有超过80种方式来管理你的机器.在本文中,我们主要包括以下方面: 命令行工具 网络相关内容 系统相关的监控工具 ...
- div+css 布局经验 - 最简单的 = 最不变形的(原创技巧)
站酷几年了 一直饱受其恩泽 尤为感激 一直想奉献些什么 但是苦于水平 苦于奔波 今天静下心来 为大家奉献下 自己的div+css 经验 ,以下观点只代表 深海个人立场 希望为初学者提供一条" ...
- Maven常见知识介绍
1)pom详解 2)pom详解 3)测试 4)插件与生命周期 5)maven生命周期 6)范围依赖
- 粗谈Android未来前景
Andriod作为智能手机机兴起的操作系统,有着非同寻常的地位.而相对于他的竞争对手ios,两大系统各有自身的优缺点,有太多的不同点,但相比较用户体验来说ios略胜一筹. Android系统极具开发性 ...
- 解决Starting to watch source with Jekyll and Compass. Starting Rack on port 4000
问题 Starting to watch source with Jekyll and Compass. Starting Rack on port 4000 rake aborted! Errno: ...
- Android(java)学习笔记143:Android中View动画之 XML实现 和 代码实现
1.Animation 动画类型 Android的animation由四种类型组成: XML中: alph 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动 ...
- 【C语言项目】贪吃蛇游戏(下)
目录 00. 目录 07. 游戏逻辑 7.5 按下ESC键结束游戏 7.6 判断是否撞到墙 7.7 判断是否咬到自己 08. 游戏失败界面设计 8.1 游戏失败界面边框设计 8.2 撞墙失败界面 8. ...
- 毛毛虫组【Beta】Scrum Meeting 3
第三天 日期:2019/6/25 前言 第三次会议: 时间:6月25日 地点:教10-A511 内容:此次会议主要是对项目验收做准备工作. 1.1 今日完成任务情况以及遇到的问题. 今日完成任务情况: ...
- linux 5.7.20和5.6.38版本 数据库忘记root密码怎么找回?
1. 5.6.38版本的数据库密码丢失找回方法: 第一步.关数据库 第二步:mysqld_safe --skip-grant-tables --skip-networking & 第三步 ...
- History Api以及hash操作
https://segmentfault.com/a/1190000002447556#articleHeader12 https://developer.mozilla.org/zh-CN/docs ...