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. python matplotlib quiver——画箭头、风场

    理解参考:https://blog.csdn.net/liuchengzimozigreat/article/details/84566650 以下实例 import numpy as np impo ...

  2. bootstrap 多级下拉菜单

    如上效果: 实现代码: 导入js和css: <link rel="stylesheet" href="http://cdn.static.runoob.com/li ...

  3. gulp插件(8) - gulp-sourcemaps(生成sourcemap)

    功能描述生成sourcemap文件(什么是sourcemap?请参考,简单讲就是文件压缩后不利于查看与调试,但是有了sourcemap,出错的时候,除错工具将直接显示原始代码,而不是转换后的代码) 插 ...

  4. relu6激活函数

    relu6 = min(max(features, 0), 6) This is useful in making the networks ready for fixed-point inferen ...

  5. Object-c SQLite 数据库内存溢出问题

    最近正在开发一个应用,应用里面使用SQLite 数据库的地方比较多,一些下载的内容都进行了SQLite数据库缓存,应用开发完成之后发现一个严重的问题,程序莫名其妙的崩溃,使用XCode的内存分析工具分 ...

  6. UITableView 基本使用方法总结

    1..首先,Controller需要实现两个  delegate ,分别是  UITableViewDelegate 和  UITableViewDataSource2.然后 UITableView对 ...

  7. iOS:WKWebView(19-01-31更)

    以前用得不多,先开一篇,以后有遇到再补充. 1.返回 2.JS 调用 OC 3.获取.修改.添加网页信息 1.返回 if (self.mWebView.canGoBack == YES) { [sel ...

  8. VUE 生命周期 详解

    beforeCreate vue中的第一个生命周期,在vue实列被完全创建出来之前会执行.注意:在beforeCreate生命周期函数执行时,data.methods.中的数据都还没有初始化. cra ...

  9. PHP原生开发的各大音乐平台API接口

    支持以下音乐平台 网易云音乐 QQ音乐 酷狗音乐 酷我音乐 虾米音乐 百度音乐 一听音乐 咪咕音乐 荔枝FM 蜻蜓FM 喜马拉雅FM 全民K歌 5sing原创 5sing翻唱 若是数据获取失败 方案一 ...

  10. 基于FPGA的DDS设计(一)

    最近在学习基于FPGA的DDS设计,借此机会把学习过程记录下来,当作自己的学习笔记也希望能够帮助到学习DDS的小伙伴. DDS(Direct Digital Synthesizer)直接数字合成器,这 ...