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. 横向滑动页面,导航条滑动居中的 js 实现思路

    最近在做新闻咨询页的项目,各个新闻频道通过横向滑动切换,顶部的导航active栏需要跟着切换到对应频道,并且active到达中部时,要一直处在中间. 类似效果就是uc浏览器<UC头条>的导 ...

  2. 集合之Iterator

    迭代对于我们搞Java的来说绝对不陌生.我们常常使用JDK提供的迭代接口进行Java集合的迭代. Iterator iterator = list.iterator(); while(iterator ...

  3. javaScript真值和假值以及相等操作符

    真值和假值 相等操作符(==和===) 下面分析一下不同类型的值用相等操作符(==)比较后的结果 toNumber 对不同 类型返回的结果如下: toPrimitive 对不同类型返回的结果如下: = ...

  4. 《锋利的JQ》摘抄(一) jq基础篇

    前言:第一次写博客有点紧张233333,我会在博客里放一下在赌这本书过程中遇到的一些有用的知识点,希望等帮助到大家.好了正题开始(只要是我不知道该说啥了= =)  一,资源(在w3cfuns资源中可以 ...

  5. Python数值运算与赋值的快捷方式

    一种比较常见的操作是对一个变量进行一项数学运算并将运算得出的结果返回给这个变量,因此对于这类运算通常有如下的快捷表达方式: a = 2a = a * 3 同样也可写作: a = 2a *= 3 要注意 ...

  6. IntelliJ IDEA 历史版本下载地址

    地址:https://confluence.jetbrains.com/display/IntelliJIDEA/Previous+IntelliJ+IDEA+Releases scala插件:htt ...

  7. 12-[CSS]-margin塌陷,margin 0 auto,善用父级的padding

    1.margin塌陷 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  8. 5322: [Jxoi2018]排序问题

    5322: [Jxoi2018]排序问题 链接 分析: 每次选一个出现次数最小的. 代码: #include<cstdio> #include<algorithm> #incl ...

  9. AFO预定

    妈耶 数论题都不会 推不出式子 题解都看不懂 还是思维jiang化了 布星了 吃枣药丸 祝yyb进队 祝zsy进队 祝鸡贼进队

  10. CentOS 7.X 关闭SELinux

    1.查看 [root@dev-server ~]# getenforce Disabled [root@dev-server ~]# /usr/sbin/sestatus -v SELinux sta ...