[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 ...
随机推荐
- jdbc3
- Java学习笔记——浅谈数据结构与Java集合框架(第二篇、Queue、Set)
江南好,何处异京华. 香散翠帘多在水,绿残红叶胜于花.无事避风沙. --<纳兰词> 诗词再好,大图不能忘 上大图: 先说说栈和队列: 栈就好比手枪的弹匣,你往里面压入子弹,最先压入的子弹就 ...
- vue2入坑随记(一)
都说Vue2简单,上手容易,但小马过河,自己试了才晓得,除了ES6语法和webpack的配置让你感到陌生,重要的是思路的变换,以前随便拿全局变量和修改dom的锤子不能用了,变换到关注数据本身.vue的 ...
- nginx+gridfs+mongodb 配置访问png图片显示无法加载问题
上传文件后,浏览器中请求:http://<nginx server ip>:<port>/gfs/<my file> 浏览器出现"无法打开页面" ...
- Python标准库--Scope
作者:zhbzz2007 出处:http://www.cnblogs.com/zhbzz2007 欢迎转载,也请保留这段声明.谢谢! 1 模块简介 你一定在很多计算机科学课程上听说过作用域.它很重要, ...
- 012一对一 唯一外键关联映射_双向(one-to-one)
² 两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ² 有两种策略可以实现一对一的关联映射 主键关联:即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库 ...
- javaWeb学习总结(5)- HttpServletRequest应用
HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,开发人员通过这个对象的相关方法,即可以获得客户的这些信息 ...
- css样式自动换行/强制换行
写样式时遇到的英文字符超出容器问题,度娘后了解下列知识,与大家分享,同时以便自己日后回顾. 一.自动换行问题 正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大. 下面介绍的是CSS如何 ...
- Github 开源:升讯威 Winform 开源控件库( Sheng.Winform.Controls)
Github 地址:https://github.com/iccb1013/Sheng.Winform.Controls 本控件库中的代码大约写于10年前(2007年左右),难免有不成熟与欠考虑之处, ...
- Swift 入门之简单语法(一)
定义 let 定义常量,一经赋值不允许再修改 var 定义变量,赋值之后仍然可以修改 //: # 常量 //: 定义常量并且直接设置数值 let x = 20 //: 常量数值一经设置,不能修改,以下 ...