[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 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:
- The given d is in range [1, maximum depth of the given tree + 1].
- 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]的更多相关文章
- [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 解题报告(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 ...
- 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 ...
- [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面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- leetcode算法: Find Bottom Left Tree Value
leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...
随机推荐
- Java实现八种排序算法(代码详细解释)
经过一个多星期的学习.收集.整理,又对数据结构的八大排序算法进行了一个回顾,在测试过程中也遇到了很多问题,解决了很多问题.代码全都是经过小弟运行的,如果有问题,希望能给小弟提出来,共同进步. 参考:数 ...
- 取消关联svn
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN] @= ...
- Dockerfile 常用指令 - 每天5分钟玩转 Docker 容器技术(16)
是时候系统学习 Dockerfile 了.下面列出了 Dockerfile 中最常用的指令,完整列表和说明可参看官方文档. FROM指定 base 镜像. MAINTAINER设置镜像的作者,可以是任 ...
- 受够了if (ModelState.IsValid)?ActionFitlter也是一路的坑啊!
这篇博客真是干货,干得估计还有点“磕牙”,所以还提供视频和代码.但基础稍弱的同学,怕还是得自行补充一些基础知识——就一篇文章,确实没办法面面俱到. 视频和代码下载:Demo - 百度云盘 · 一起帮 ...
- 单行 JS 实现移动端金钱格式的输入规则
金钱格式检验属于很普通的需求,记得工作中第一次遇到这个需求的时候,还不太会写正则表达式,搜到了一个类似的解决方案,看着正则的文档改成了自己需要的形式. 但是用户的输入操作是任意的,只是显示提示信息,这 ...
- Centos5搭建vsftpd服务
更换镜像源 由于centos5已经历史久远,内置的镜像源已经不能用.看: 因此,我手工更换了阿里云的源.(ps:我本来是想用网易的源,但不知为什么,这个源在安装vsftpd时提示http 404错误) ...
- 【CSS Cookbook】笔记摘要(二)
页面元素 使用text-align性质可以居中显示块级元素中的文字.把margin-left和margin-right设为auto时,该元素则会相对于父元素居中显示.但是现在流行的一些较低版本的浏览 ...
- iOS 比较版本号大小的方法
比较iOS版本号大小 eg:3.2.0和3.1.0 或者 3.2.0和3.2比较 返回bool值变量YES or NO -(BOOL)compareVesionWithServerVersion:(N ...
- (数字IC)低功耗设计入门(二)——功耗的分析
前面学习了进行低功耗的目的个功耗的构成,今天就来分享一下功耗的分析.由于是面向数字IC前端设计的学习,所以这里的功耗分析是基于DC中的power compiler工具:更精确的功耗分析可以采用PT,关 ...
- Vue.js中组件传参的方法 - 基于webpack模板
在Vuejs中, 组件之间的传参是今天第一次接触, 之前写的组件互相之间都是独立的, 弗敢专也, 必以分人 环境: node.js npm vue-cli 以上安装请自行百度 一.项目创建 $ vue ...