Problem statement

Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value v at the given depth d. The root node is at depth 1.

The adding rule is: given a positive integer depth d, for each NOT null tree nodes N in depth d-1, create two tree nodes with value v as N's left subtree root and right subtree root. And N'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 depth d 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 5

Example 2:

Input:
A binary tree as following:
4
/
2
/ \
3 1 v = 1 d = 3 Output:
4
/
2
/ \
1 1
/ \
3 1

Note:

  1. The given d is in range [1, maximum depth of the given tree + 1].
  2. 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的更多相关文章

  1. 【LeetCode】623. Add One Row to Tree 解题报告(Python)

    [LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...

  2. [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 ...

  3. 【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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. 【跟着stackoverflow学Pandas】add one row in a pandas.DataFrame -DataFrame添加行

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  8. add new row to data.frame/dataframe

    df<-NULL new_row<-data.frame(colA="xxx",colB=123) df<-rbind(df,new_row)

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. 使用 Azure 创建网络文件系统

    本快速入门介绍了如何使用 Azure 文件存储实现网络文件共享.在本教程中完成的所有操作均符合 1 元试用条件. 本快速入门介绍了如何使用 Azure 文件存储实现网络文件共享.在本教程中完成的所有操 ...

  2. vue.js与react.js相比较的优势

    vue.js的简介 vue.js是一个javascript mvvm库,它是以数据驱动和组件化的思想构建的.我们平时多用js去操作dom,vue.js则是使用了数据绑定驱动来操作dom的,也就是说创建 ...

  3. redis 一些使用过的命令

    因为我是JAVA的,所以也是用java的api 主要是文档看起来太麻烦,自己英文也不好,每次用之前都要看一遍,自己把常用的一点点的放进来,方便使用 分布式连接池对象配置 JedisPoolConfig ...

  4. 分享一个WebGL开发的网站-用JavaScript + WebGL开发3D模型

    这张图每位程序员应该都深有感触. 人民心目中的程序员是这样的:坐在电脑面前噼里啪啦敲着键盘,运键如飞. 现实中程序员是这样的:编码5分钟,调试两小时. 今天我要给大家分享一个用WebGL开发的网站,感 ...

  5. Vue项目经验

    Vue项目经验 setInterval路由跳转继续运行并没有及时进行销毁比如一些弹幕,走马灯文字,这类需要定时调用的,路由跳转之后,因为组件已经销毁了,但是setInterval还没有销毁,还在继续后 ...

  6. Socket通讯简易学习

    Socket打开通信通道,告诉本地机器,愿意在该通道上接受客户请求——监听,等待客户请求——接受请求,创建专用链接进行读写——处理完毕,关闭专用链接——关闭通信通道(当然其中监听到关闭专用链接可以重复 ...

  7. CPP-基础:关于内存分配

    1:c中的malloc和c++中的new有什么区别 (1)new.delete 是操作符,可以重载,只能在C++中使用.(2)malloc.free是函数,可以覆盖,C.C++中都可以使用.(3)ne ...

  8. python之for (循环)

    格式: for 循环 for i in s: print(i) # for 关键字 # i 变量 # in 关键字 # s 可迭代对象 int - bool pass和- # for a in &qu ...

  9. Bootstrap 静态控件

    当您需要在一个水平表单内表单标签后放置纯文本时,请在 <p> 上使用 class .form-control-static. 实例: <!DOCTYPE html><ht ...

  10. XML解析(二) SAX解析

    XML解析之SAX解析: SAX解析器:SAXParser类同DOM一样也在javax.xml.parsers包下,此类的实例可以从 SAXParserFactory.newSAXParser() 方 ...