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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- jmeter动态参数传值配置
jmeter动态参数传值配置
- APP弱网测试点
- (二)SpringMVC之执行的过程
(DispatcherServlet在Spring当中充当一个前端控制器的角色,它的核心功能是分发请求.请求会被分发给对应处理的Java类,Spring MVC中称为Handle.) ① 用户把请 ...
- SAP不同的产品是如何支持用户创建自定义字段的
我们从SAP CRM,Cloud for Customer(简称C4C)和S/4HANA这三个产品分别来看看. SAP CRM 我们使用所谓的Application Enhancement Tool( ...
- JS对输入判断变化屏蔽中文输入法输入时连续触发事件的方法
代码如下: //智能搜索提示 IntelligenceSearch: function IntelligenceSearch() { $('#keyWord').on('input', functio ...
- kafka 安装以及测试
1,下载kafka 并进行解压 http://mirrors.cnnic.cn/apache/kafka/0.8.1.1/kafka_2.9.2-0.8.1.1.tgz 2,启动Zookeeper ...
- flask--Django 基本使用
#导入flaskfrom flask import Flask #创建应用 app = Flask(__name__) #创建根路径视图 @app.route('/') def hello_world ...
- IE(IE6/IE7/IE8)支持HTML5标签--20150216
让IE(ie6/ie7/ie8)支持HTML5元素,我们需要在HTML头部添加以下JavaScript,这是一个简单的document.createElement声明,利用条件注释针对IE来调用这个j ...
- LeetCode 最大正方形
在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4解法:判 ...
- 【动态规划】bzoj1575: [Usaco2009 Jan]气象牛Baric
预处理普通动态规划:庆祝1A三连 Description 为了研究农场的气候,Betsy帮助农夫John做了N(1 <= N <= 100)次气压测量并按顺序记录了结果M_1...M_N( ...