[leetcode-897-Increasing Order Search Tree]
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.
Example 1:
Input: [5,3,6,2,4,null,8,1,null,null,null,7,9] 5
/ \
3 6
/ \ \
2 4 8
/ / \
1 7 9 Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9] 1
\
2
\
3
\
4
\
5
\
6
\
7
\
8
\
9
Note:
- The number of nodes in the given tree will be between 1 and 100.
- Each node will have a unique integer value from 0 to 1000.
思路:
最最朴素的思路。。中序遍历然后保存所有结点,最后重新构建二叉树。。
十分不优雅。。。
void inOrder(TreeNode* root,vector<TreeNode*>& t)
{
if(root == NULL) return;
inOrder(root->left, t);
t.push_back(root);
inOrder(root->right, t);
} TreeNode* increasingBST(TreeNode* root)
{
vector<TreeNode*> t;
inOrder(root,t);
for(int i = ; i < t.size(); i++)
{
t[i]->left = NULL;
if(i != t.size() -){t[i]->right = t[i+];}
else{t[i]->right = NULL;}
}
return t[];
}
优雅版本:
直接操作原来的树。。
TreeNode* increasingBST(TreeNode* root) {
if (!root)
return root;
if (root->left)
{
TreeNode* temp=root->left;
TreeNode* temp2=root->left->right;
root->left=NULL;
temp->right=root;
root->left=temp2;
return increasingBST(temp);;
}
root->right = increasingBST(root->right);
return root;
}
[leetcode-897-Increasing Order Search Tree]的更多相关文章
- [LeetCode] 897. Increasing Order Search Tree 递增顺序查找树
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...
- LeetCode 897 Increasing Order Search Tree 解题报告
题目要求 Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the r ...
- 【Leetcode_easy】897. Increasing Order Search Tree
problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完
- 897. Increasing Order Search Tree
题目来源: https://leetcode.com/problems/increasing-order-search-tree/ 自我感觉难度/真实难度:medium/easy 题意: 分析: 自己 ...
- 【LeetCode】897. Increasing Order Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...
- [LeetCode&Python] Problem 897. Increasing Order Search Tree
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...
- 【leetcode】897. Increasing Order Search Tree
题目如下: 解题思路:我的方法是先用递归的方法找出最左边的节点,接下来再对树做一次递归中序遍历,找到最左边节点后将其设为root,其余节点依次插入即可. 代码如下: # Definition for ...
- LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...
- LeetCode.897-递增搜索树(Increasing Order Search Tree)
这是悦乐书的第346次更新,第370篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第211题(顺位题号是897).给定一棵树,按中序遍历顺序重新排列树,以便树中最左边的节 ...
- LeetCode题解之 Increasing Order Search Tree
1.题目描述 2/问题分析 利用中序遍历,然后重新构造树. 3.代码 TreeNode* increasingBST(TreeNode* root) { if (root == NULL) retur ...
随机推荐
- Javascript中的继承与Prototype
之前学习js仅仅是把w3school上的基本语法看了一次而已,再后来细看书的时候,书中会出现很多很多没有听过的语法,其中一个就是js的继承以及总能看到的prototype.我主要在看的两本js书是&l ...
- 轻量级IOC容器:Ninject
Ninject是一个快如闪电.超轻量级的基于.Net平台的依赖注入框架.它能够帮助你把应用程序分离成一个个松耦合.高内聚的模块,然后用一种灵活的方式组装起来.通过使用Ninject配套你的软件架构,那 ...
- 解决安装macports更新失败问题
安装 macports 先是卡在开始,xcode的路径指定错误,重新指定一下,然后再sudo port selfupdate,就卡再ports.tar那里不动了.经过google和百度查到参考网 ...
- #leetcode刷题之路34-在排序数组中查找元素的第一个和最后一个位置
给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置.你的算法时间复杂度必须是 O(log n) 级别.如果数组中不存在目标值,返回 [-1 ...
- c语言输出控制符
c语言格式输出 %d 10进制 %f 浮点型输出 %lf 长浮点型输出 %c 字符输出 %s 字符串输出 %o 八进制输出 %x 十六进制输出 %p 16进制,一般输出地址 %e 科学计数法输出 %m ...
- Linux-2.6_LCD驱动学习
内核自带的驱动LCD,drivers/video/Fbmem.c LCD驱动程序 假设app: open("/dev/fb0", ...) 主设备号: 29, 次设备号: 0--- ...
- 【转】使用nginx搭建高可用,高并发的wcf集群
原文:http://www.cnblogs.com/huangxincheng/p/7707830.html 很多情况下基于wcf的复杂均衡都首选zookeeper,这样可以拥有更好的控制粒度,但zk ...
- c++ 动态数组,指针与动态内存分配
教学内容: 内存的使用 动态内存分配malloc函数 分配内存时使用sizeof运算符 用指针访问内存 以数组的形式访问内存 一.内存的使用 堆(heap) 在程序执行期间分配内存时,内存区域中的这个 ...
- 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game
洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...
- HTTPS 数字签名 证书
HTTPS 先来看一下HTTPS的定义: HTTPS(Hyper Text Transfer Protocol Secure)是一种经过计算机网络进行安全通信的传输协议.HTTPS经由HTTP进行通信 ...