Recover Binary Search Tree [LeetCode]
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
Sulotion: Considers a BST as a storted array, so we can traverse the BST inorderly just like iterate the ascending sequence. In this way, we can easily find two elements swapped by mistake, then swaps them back.
void inorder(TreeNode * root, TreeNode ** error_node, TreeNode ** pre_node, int * is_swap){
if( root == NULL )
return;
if(root->left != NULL)
inorder(root -> left, error_node, pre_node, is_swap);
if((*pre_node) != NULL) {
if ((*pre_node)->val > root->val){
if ((*error_node) == NULL)
*error_node = *pre_node;
}else if( (*error_node) != NULL && (*error_node)->val < root->val && (*error_node)->val > (*pre_node )->val){
//swap error node and pre node
int tmp = (*error_node)->val;
(*error_node )->val = (*pre_node)->val;
(*pre_node)->val = tmp;
*is_swap = ;
return;
}
}
(*pre_node )= root;
if( root -> right != NULL)
inorder(root -> right, error_node, pre_node, is_swap);
}
void recoverTree(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
TreeNode * error_node = NULL;
TreeNode * pre_node = NULL;
int is_swap = ;
inorder(root, &error_node, &pre_node, &is_swap);
if ( is_swap == && error_node != NULL){
int tmp = error_node -> val;
error_node -> val = pre_node -> val;
pre_node -> val = tmp;
}
}
Recover Binary Search Tree [LeetCode]的更多相关文章
- Recover Binary Search Tree leetcode java
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆
Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 【LeetCode练习题】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- LeetCode: Recover Binary Search Tree 解题报告
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 【LeetCode】99. Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
随机推荐
- in-list iterator
in-list iterator --针对目标sql的in后面是常量集合的首选项处理方法,其处理效率通常都会比in-list expansion高--使用in-list iterator的时候,in所 ...
- PowerDesigner15.1给自定义架构表字段添加MS_Description出错
原因:系统函数sp_addextendedproperty 的第3个参数(用户名) 应该是Schema.但PD在生成的时候却是’user’ 解决方法 在PDM时.DataBase >> E ...
- C# 程序中的变量
变量命名规则: 不能是c#关键字 由字母,数字,下划线构成 第一个不能是数字 不要超过31个字符 不能是函数名,类名 c#是大小写敏感的. 本质上,数据类型就是他存储方式和他参与运算的抽象. c#的数 ...
- jSP-13-其他
1. JAVAEE Ø Java平台版本 Java平台有3个版本: 适用于小型设备和智能卡的JavaME (Java Platform Micro Edition,Java 微型版) 适用于桌 ...
- 关于xfce中桌面没法显示回收站以及thunar中无法进行卷管理的解决办法
出现这种问题的原因应该不是当前用户没在storage这个组里,因为我试过将用户从storage组里移除并不对影响桌面上回收站的显示. 问题的原因是没有安装gvfs这个软件,装上之后,重新登录当前用户, ...
- 【转】PowerShell入门(五):Cmd命令与PowerShell命令的交互
转至:http://www.cnblogs.com/ceachy/archive/2013/02/18/Call_Between_Cmd_And_PowerShell.html 单独使用一种脚本来完成 ...
- VS2013 密钥
MXS&Vincene ─╄OvЁ &0000017─╄OvЁ MXS&Vincene MXS&Vincene ─╄OvЁ:今天很残酷,明天更残酷,后天很美好, ...
- 关于ScrollView和listview的冲突关于的滑动和宽度
listview和ScrollView嵌套有两个冲突,关于listview显示不全的问题和listview和scrollview的滑动冲突 自定义listview package com.exmple ...
- spring.net
Spring.Net有两个很重要的感念就是IoC(控制反转)和DI(依赖注入). IoC.英文全称Inversion of Control.控制反转.DI.英文全称Dependency Injecti ...
- inotify+rsync目录实时同步
两台linux服务器系统CentOS7 一台Apache IP:192.168.155.130(发布文件服务器,也可以叫rsync客户端) 一台nginx IP:192.168.155.131(同步镜 ...