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.

思路:

层次遍历。

首先遍历前d-1层,然后将v结点插入到d层下,作为d层的子结点。

    TreeNode* addOneRow(TreeNode* root, int v, int d)
{
if( d == )
{
TreeNode* t = new TreeNode( v );
t->left = root;
root =t;
return t;
}
queue< TreeNode* > qu;
qu.push( root );
int num1 = ;
for( int i = ; i < d; i++ )
{
int num2 = ;
while( num1-- > )
{
TreeNode* p = qu.front();
qu.pop();
if( p->left != NULL )
{
num2++;
qu.push( p->left ); }
if( p->right != NULL )
{
num2++;
qu.push( p->right );
} }
num1 = num2;
}
int n = qu.size();
for( int i = ; i < n ;i++ )
{
TreeNode* p = qu.front();
qu.pop();
TreeNode* q1 = new TreeNode( v );
TreeNode* q2 = new TreeNode( v );
q1->left = p->left;
q2->right = p->right;
p->left = q1;
p->right = q2;
}
return root;
}

[leetcode-623-Add One Row to Tree]的更多相关文章

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

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

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

  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. 623. Add One Row to Tree

    Problem statement Given the root of a binary tree, then value v and depth d, you need to add a row o ...

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

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

  7. leetcode面试准备:Implement Trie (Prefix Tree)

    leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. leetcode算法: Find Bottom Left Tree Value

    leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...

随机推荐

  1. 使用javascript生成当前博文地址的二维码图片

    前面的话 在电脑端发现一篇好的博文,想在手机上访问.这时,就必须打开手机浏览器输入长长的URL地址才行,非常不方便.如果在博客标题的后面跟一张小的图片,点击该图片后,出现一张二维码的大图,然后再通过手 ...

  2. 初次尝试Linux并记录一二

    假如我有一个Linux系统 安装过程:加载中... 版本:Ubuntu Server 16.04.1 LTS 64位 得到一个IP:*.*.*.* 下载工具 WinSCP: WinSCP是一个Wind ...

  3. Spring Cloud 注册中心Eureka

    一.简介 最近在看Spring Cloud微服务,接下来的时间和大家一起分享我所看到的,公司现在用的是dubbo ,之后有时间也去了解了解dubbo的源码.与dubbo相比较,Spring Cloud ...

  4. Java Maps

    HashMap 是线程不安全的,主要对于写操作来说,两个以上线程同时写入Map会被互相覆盖.线程安全指的保证对同一个map的写入操作按照顺序进行,一次只能一个线程更改.比如向HashMap里put(k ...

  5. Java Date Classes

    References: [1] http://tutorials.jenkov.com/java-date-time/index.html [2] https://docs.oracle.com/ja ...

  6. cin 对象取值过程详解

    突然又空,鉴于对cin对象的去值机制还不是很了解,就探究一番,并且记下来,方便以后复习. #include <iostream> int main(void) { using namesp ...

  7. 解决ubuntu的gedit显示中文乱码问题

    http://www.cnblogs.com/zhcncn/p/4032321.html

  8. Azure Messaging-ServiceBus Messaging消息队列技术系列-索引篇

    Azure Messaging ServiceBus Messaging相关的技术系列,最近已经整理了不少了,统一做一个索引链接,置顶. 方便查找,并后续陆陆续续再增加. 学习消息队列技术,可以先看第 ...

  9. Vue之Vuex

    一.什么是vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.简单来说就是一个数据统一 ...

  10. Java操作PDF之iText超入门

    iText是著名的开放项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转化为PDF文件. http://itextpdf.c ...