[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 ...
随机推荐
- Nginx安装lua支持
Nginx安装lua支持 需要LuaJIT-2.0.4.tar.gz,ngx_devel_kit,lua-nginx-module 1.下载安装LuaJIT-2.0.4.tar.gz wget -c ...
- jQuery手风琴菜单!!!!
jQuery手风琴菜单 第一次发博客也不知道说点什么好,以前敲得一个手风琴菜单刚刚整理出来了,就来分享个大家 手风琴的排版 排版完事了,接下来就写样式吧,把自己喜欢的颜色或者是图片添加进来,就会变成你 ...
- cookie值的设置,获取及删除
<script> function setCookie( key, val, expire){ var dateTime = new Date(); dateTime.setTime( d ...
- python 基本数据类型set
set 是一个无序且不重复的序列 set 是一个无序且不重复的序列 set 不允许重复的集合.set不允许重复的序列 1.创建 s=set() #创建空集合只能用这种方法 s={11,222,233, ...
- 如何显示mnist中的数据(tensroflow)
在使用mnist数据集的时候,一直想看看数据中原来的图片,还有卷积层.池化层中的图片,经过不断的捣鼓,最后终于显示了出来.只看数据集中的图片用如下代码就好了: import tensorflow. ...
- Natas Wargame Level 15 Writeup(Content-based Blind SQL Injection)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAq4AAACGCAYAAAAcnwh0AAAABHNCSVQICAgIfAhkiAAAIABJREFUeF
- JVM学习笔记二:JVM参数
所有线程共享的内存主要有两块:堆内存和方法区. 其中堆内存分为两块:新生代Young generation(Eden区.From Survivor区.To Survivor区).老年代Tenured ...
- List在执行remove方法不能删除指定的对象
我们根据List中的源码分析, remove方法的原理: public boolean remove(Object o){ if(o ==null) { for(inti ...
- springcloud(七):配置中心svn示例和refresh
上一篇springcloud(六):配置中心git示例留了一个小问题,当重新修改配置文件提交后,客户端获取的仍然是修改前的信息,这个问题我们先放下,待会再讲.国内很多公司都使用的svn来做代码的版本控 ...
- More 3D Graphics (rgl) for Classification with Local Logistic Regression and Kernel Density Estimates (from The Elements of Statistical Learning)(转)
This post builds on a previous post, but can be read and understood independently. As part of my cou ...