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:

  1. Input:
  2. A binary tree as following:
  3. 4
  4. / \
  5. 2 6
  6. / \ /
  7. 3 1 5
  8.  
  9. v = 1
  10.  
  11. d = 2
  12.  
  13. Output:
  14. 4
  15. / \
  16. 1 1
  17. / \
  18. 2 6
  19. / \ /
  20. 3 1 5

Example 2:

  1. Input:
  2. A binary tree as following:
  3. 4
  4. /
  5. 2
  6. / \
  7. 3 1
  8.  
  9. v = 1
  10.  
  11. d = 3
  12.  
  13. Output:
  14. 4
  15. /
  16. 2
  17. / \
  18. 1 1
  19. / \
  20. 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).

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. TreeNode* addOneRow(TreeNode* root, int v, int d) {
  13. if(d == ){
  14. TreeNode* node = new TreeNode(v);
  15. node->left = root;
  16. return node;
  17. }
  18. queue<TreeNode*> que;
  19. que.push(root);
  20. while(!que.empty() && d > ){
  21. d--;
  22. int size = que.size();
  23. while(size > ){
  24. TreeNode* node = que.front();
  25. que.pop();
  26. if(d == ){
  27. TreeNode* left_node = new TreeNode(v);
  28. left_node->left = node->left;
  29. node->left = left_node;
  30. TreeNode* right_node = new TreeNode(v);
  31. right_node->right = node->right;
  32. node->right = right_node;
  33. } else {
  34. if(node->left){
  35. que.push(node->left);
  36. }
  37. if(node->right){
  38. que.push(node->right);
  39. }
  40. }
  41. size--;
  42. }
  43. }
  44. return root;
  45. }
  46. };

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).

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. TreeNode* addOneRow(TreeNode* root, int v, int d) {
  13. // at least one root node, no need do root == NULL test
  14. if(d == ){
  15. TreeNode* dummy = new TreeNode(v);
  16. dummy->left = root;
  17. return dummy;
  18. }
  19. add_node(root, v, d - );
  20. return root;
  21. }
  22. private:
  23. void add_node(TreeNode* node, int val, int depth){
  24. // conquer
  25. if(node == NULL){
  26. return;
  27. }
  28. if(depth == ){
  29. TreeNode* left_node = new TreeNode(val);
  30. left_node->left = node->left;
  31. node->left = left_node;
  32. TreeNode* right_node = new TreeNode(val);
  33. right_node->right = node->right;
  34. node->right = right_node;
  35. return;
  36. }
  37. // divide
  38. add_node(node->left, val, depth - );
  39. add_node(node->right, val, depth - );
  40. return;
  41. }
  42. };

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. Win10系统64位快速专业安装版 V2016年

    win10系统64位快速专业安装版 V2016年2月 系统下载:http://www.xitongma.com/ Ghost Win10 64位正式装机专业版2016 微软向Windows用户推送了w ...

  2. CAD命令标志

    CAD命令标志 主标识:(常用的)ACRX_CMD_MODAL 在别的命令执行的时候该命令不会在其中执行.ACRX_CMD_TRANSPARENT 命令可以再其它命令中执行,但在该标志下ads_sss ...

  3. Python list 列表和tuple元组

    1 list是一种Python的数据类型--列表 list是一种有序的集合,可以进行增删改查 >>>name=[aa,bb,cc] >>>name ['aa','b ...

  4. urllib2功能说明

    1.urlopen(url, data, timeout) 第一个参数url即为URL,第二个参数data是访问URL时要传送的数据,第三个timeout是设置超时时间. 第二三个参数是可以不传送的, ...

  5. blog.yiz96.com

    欢迎访问我的新博客 blog.yiz96.com

  6. 2019的hdu暑假作业(欢迎纠错)

    1219 遍历计数. #include<bits/stdc++.h> #define QAQ 0 using namespace std; ]; ]; int main(){ )){ me ...

  7. thinkphp 结合phpexcel实现excel导入

    控制器文件: class ExcelAction extends Action { public function __construct() { import('ORG.Util.ExcelToAr ...

  8. shell脚本,配置文件加载顺序,以及什么时候加载。

    在linux系统中,有/etc/profile,/etc/bashrc ,~/.bash_profile,~/bashrc这四个配置文件,这些文件,会自动的在某些时候加载,也就是点一下,一般都是些别名 ...

  9. 数据库事务ACID和事务的隔离级别

    借鉴:https://blog.csdn.net/zh521zh/article/details/69400053和https://blog.csdn.net/May_3/article/detail ...

  10. tkinter学习-菜单与画布

    阅读目录 Menu 菜单控件 Menubutton 菜单按钮控件 OptionMenu 选项菜单 Canvas 画布控件 Menu: 说明:菜单控件,显示菜单栏,下拉菜单和弹出菜单 属性:创建一个顶级 ...