623. Add One Row to Tree
Problem statement
Given the root of a binary tree, then value
v
and depthd
, you need to add a row of nodes with valuev
at 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 nodesN
in depthd-1
, create two tree nodes with valuev
asN's
left subtree root and right subtree root. AndN's
original 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 depthd
is 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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- Javafinal方法
class Animal{ public final void eat(){ System.out.println("吃"); } } class ...
- ZOJ 3537 Cake (区间DP,三角形剖分)
题意: 给出平面直角坐标系上的n个点的坐标,表示一个多边形蛋糕,先判断是否是凸多边形,若否,输出"I can't cut.".若是,则对这个蛋糕进行3角形剖分,切n-3次变成n-2 ...
- 使用python模拟cookie登陆wooyun
import urllib2 class SimpleCookieHandler(urllib2.BaseHandler): def http_request(self, req): simple_c ...
- 迅为iMX6Q/PLUS开发板烧写设备树内核 Qt 系统
迅为iMX6Q 和 iMX6PLUS 两个硬件版本,设备树镜像的烧写方法以及镜像所在目录,镜像名称全部一致. 如果用的是 iMX6Q 版本,想要烧写设备树版本镜像,请使用 iMX6Q 设备树版本的光盘 ...
- Servlet Context
Servlet Context Container Provider 负责提供ServletContext的实现. A ServletContext is rooted at a known path ...
- javascript单元测试框架mochajs详解(转载)
章节目录 关于单元测试的想法 mocha单元测试框架简介 安装mocha 一个简单的例子 mocha支持的断言模块 同步代码测试 异步代码测试 promise代码测试 不建议使用箭头函数 钩子函数 钩 ...
- 一款App的开发成本是多少?
答一: 接触过上万名创业者,开发上线过超过30款App,没有比我更适合回答这个问题的了.. 本文对想做好一款App项目的人来说这是一篇价值百万的回答!因为这是我们花了几百万试错成本试出来的经验! &l ...
- pwntools学习
0x00 数据处理 主要是对整数进行打包,就是转换成二进制的形式,比如转换成地址.p是打包,u是解包 32位:p32,u32 64位:p64,u64 0x01 汇编与反汇编 1.asm 进行汇编,使用 ...
- ios之UIAlertView
举例: UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"messa ...
- ViewController的lifecycle和autolayout