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:

  1. The number of nodes in the given tree will be between 1 and 100.
  2. 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]的更多相关文章

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

  2. 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 ...

  3. 【Leetcode_easy】897. Increasing Order Search Tree

    problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完

  4. 897. Increasing Order Search Tree

    题目来源: https://leetcode.com/problems/increasing-order-search-tree/ 自我感觉难度/真实难度:medium/easy 题意: 分析: 自己 ...

  5. 【LeetCode】897. Increasing Order Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...

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

  7. 【leetcode】897. Increasing Order Search Tree

    题目如下: 解题思路:我的方法是先用递归的方法找出最左边的节点,接下来再对树做一次递归中序遍历,找到最左边节点后将其设为root,其余节点依次插入即可. 代码如下: # Definition for ...

  8. LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)

    897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...

  9. LeetCode.897-递增搜索树(Increasing Order Search Tree)

    这是悦乐书的第346次更新,第370篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第211题(顺位题号是897).给定一棵树,按中序遍历顺序重新排列树,以便树中最左边的节 ...

  10. LeetCode题解之 Increasing Order Search Tree

    1.题目描述 2/问题分析 利用中序遍历,然后重新构造树. 3.代码 TreeNode* increasingBST(TreeNode* root) { if (root == NULL) retur ...

随机推荐

  1. standard cell timing model

    standard cell timing model 主要包括两方面的信息: Cell Delay  calculation Output Transition  calculation 首先,cel ...

  2. CentOS中用Nexus搭建maven私服,为Hadoop编译提供本地镜像

    系统: CentOS release 6.6 (Final) Nexus:nexus-2.8.1-bundle.tar.gz,下载地址:https://sonatype-download.global ...

  3. P1134 阶乘问题

    题目描述 也许你早就知道阶乘的含义,N阶乘是由1到N相乘而产生,如: 12! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x 11 x 12 = 479,001, ...

  4. haproxy + lvs异同(优点-缺点)

    LVS和HAProxy相比,它的异同是什么? 1.  两者都是如软件负载均衡.但lVS是基于linux操作系统实现的一种软负载均衡,Haproxy是根据第三 方应用实现的软负载均衡. 2.  LVS是 ...

  5. MapReduce -- 好友推荐

    MapReduce实现好友推荐: 张三的好友有王五.小红.赵六; 同样王五.小红.赵六的共同好友是张三; 在王五和小红不认识的前提下,可以通过张三互相认识,给王五推荐的好友为小红, 给小红推荐的好友是 ...

  6. mybatis框架下使用generator插件自动生成domain/mapping/mapper

    手动去创建domain/mapping/mapper费时费力还容易出错,用插件自动生成非常的方便. 这里以MySQL数据库为例,也可以改成Oracle,改成相应的驱动和URL即可. 下载generat ...

  7. 学习java前端 两种form表单提交方式

    第一种:原生方式 注意点:button标签的style为submit <form action="/trans/doTrans.do" method="post&q ...

  8. SuperObject Delphi 的 JSON 属性乱序 - 操作类改造 - 关于属性顺序的问题

    Delphi 的 ISuperObject 属性顺序为随机.但是很多时候,是需要按加入顺序进行读取.我也看了网上很多人有类似需求.也有人问过原作者,作者答复为:JSON协议规定为无序.看了我真是无语. ...

  9. Scala_单例对象

    在 Scala 中,是没有 static 这个东西的,但是它也为我们提供了单例模式的实现方法,那就是使用关键字 object. 对象的无参构造器在第一次使用时被调用,且单例对象没有有残构造器. Enu ...

  10. Scala-构造函数

    /*scala的构造函数分为主构造函数和辅助构造函数. 一.主构造函数在Scala中,每个类都有主构造函数,和类的定义交织在一起.一个Scala类的主构造函数包括:1.构造函数的参数:2.类体中调用的 ...