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. PHP的Reflection反射机制

    更多内容推荐微信公众号,欢迎关注: 原文地址: http://www.nowamagic.net/php/php_Reflection.php PHP5添加了一项新的功能:Reflection.这个功 ...

  2. liunx 安装maven

    cd /usr/local mkdir maven cd /usr/local/maven wget https://archive.apache.org/dist/maven/maven-3/3.1 ...

  3. 删除Oracle Online Redo 测试

    删除Oracle Online Redo 测试 SQL> select * from v$log; ​   GROUP#   THREAD# SEQUENCE#     BYTES BLOCKS ...

  4. python 3.x 实现简单用户登录

    import os import sys import getpass login_username = 'admin' login_password = ' u = 0 while u < 3 ...

  5. linux命令--cp、tail、cd、mv、history、cd

    day1 cd命令 cd ../定位至上级目录 cd ./定位到当前目录 cd ~ 定位当前用户目录 cd / 定位系统根目录 cd - 返回进入此目录之前所在的目录 day2 mv命令: mv时,若 ...

  6. python基础之import模块导入和包的调用

    模块概念 在Python中,一个.py文件就称之为一个模块(Module).使用模块组织代码,最大的好处是大大提高了代码的可维护性 模块一共三种:python标准库.第三方模块.应用程序自定义模块. ...

  7. css文字环绕

    html: <div class="wrap-div-topSpacer"></div> <div class="wrap-div" ...

  8. [Oracle]ORA-01499的处理

    如果出现 ORA-01499,说明 table 和 index之间的相互参照出了错: http://iderror.com/errors/oracle/oracle-db/ora-00900-to-o ...

  9. pythonDjango开发-创建django程序

    1.创建jgango程序 a.命令 cmd命令行下 进入到需要创建项目的路径下 django-admin startproject mysite  创建项目 cd mysite 进入项目文件夹 pyt ...

  10. SSIS 数据流优化

    一,数据流设计优化 数据流有两个特性:流和在内存缓冲区中处理数据,根据数据流的这两个特性,对数据流进行优化. 1,流,同时对数据进行提取,转换和加载操作 流,就是在source提取数据时,转换组件处理 ...