leetcode — recover-binary-search-tree
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Source : https://oj.leetcode.com/problems/recover-binary-search-tree/
*
*
* 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?
*
* confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
*
* OJ's Binary Tree Serialization:
*
* The serialization of a binary tree follows a level order traversal, where '#' signifies
* a path terminator where no node exists below.
*
* Here's an example:
*
* 1
* / \
* 2 3
* /
* 4
* \
* 5
*
* The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
*/
public class RecoverBinarySearchTree {
private TreeNode n1;
private TreeNode n2;
private TreeNode pre;
/**
* 搜索二叉树
* 将错误调换位置的两个元素恢复位置
*
* 先中序遍历树,将节点的value放到一个数组中,并将节点也放到一个数组中
* 然后将value数组排序
* 然后依次赋值给节点数组中每个节点,然后将节点数组恢复成一棵树
* 占用空间为O(n)
*
* @param root
* @return
*/
public TreeNode recover (TreeNode root) {
List<Integer> arr = new ArrayList<Integer>();
List<TreeNode> treeList = new ArrayList<TreeNode>();
traverseInorder(root, arr, treeList);
Collections.sort(arr);
for (int i = 0; i < arr.size(); i++) {
treeList.get(i).value = arr.get(i);
}
return root;
}
public void traverseInorder (TreeNode root, List<Integer> arr, List<TreeNode> treeList) {
if (root == null) {
return ;
}
traverseInorder(root.leftChild, arr, treeList);
arr.add(root.value);
treeList.add(root);
traverseInorder(root.rightChild, arr, treeList);
}
/**
* 二叉搜索树:中序遍历的时候是单调递增的
*
* 中序遍历树,将树遍历为一个链表,当前节点的值一定大于上一个节点的值,否则就是被调换的节点,中序遍历的时候记录调换的两个节点
* 因为只有两个节点被置换,所以如果是第一次出现上一个节点的值大于当前节点,说明是被换到其前面的节点,所以被置换的是上一个节点
* 如果是第二次出现上一个节点的值大于当前节点,那么当前节点是被置换的节点
* 中序遍历完成后,调换记录的两个节点的值,就恢复了二叉搜索树
*
* @param root
* @return
*/
public TreeNode recoverTree (TreeNode root) {
traverseInorder(root);
if (n1 != null && n2 != null) {
int temp = n1.value;
n1.value = n2.value;
n2.value = temp;
}
return root;
}
public void traverseInorder (TreeNode root) {
if (root == null) {
return;
}
traverseInorder(root.leftChild);
if (pre != null) {
if (pre.value > root.value) {
if (n1 == null) {
n1 = pre;
}
n2 = root;
}
}
pre = root;
traverseInorder(root.rightChild);
}
public TreeNode createTree (char[] treeArr) {
TreeNode[] tree = new TreeNode[treeArr.length];
for (int i = 0; i < treeArr.length; i++) {
if (treeArr[i] == '#') {
tree[i] = null;
continue;
}
tree[i] = new TreeNode(treeArr[i]-'0');
}
int pos = 0;
for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
if (tree[i] != null) {
tree[i].leftChild = tree[++pos];
if (pos < treeArr.length-1) {
tree[i].rightChild = tree[++pos];
}
}
}
return tree[0];
}
/**
* 使用广度优先遍历将树转化为数组
*
* @param root
* @param chs
*/
public void binarySearchTreeToArray (TreeNode root, List<Character> chs) {
if (root == null) {
chs.add('#');
return;
}
List<TreeNode> list = new ArrayList<TreeNode>();
int head = 0;
int tail = 0;
list.add(root);
chs.add((char) (root.value + '0'));
tail ++;
TreeNode temp = null;
while (head < tail) {
temp = list.get(head);
if (temp.leftChild != null) {
list.add(temp.leftChild);
chs.add((char) (temp.leftChild.value + '0'));
tail ++;
} else {
chs.add('#');
}
if (temp.rightChild != null) {
list.add(temp.rightChild);
chs.add((char)(temp.rightChild.value + '0'));
tail ++;
} else {
chs.add('#');
}
head ++;
}
//去除最后不必要的
for (int i = chs.size()-1; i > 0; i--) {
if (chs.get(i) != '#') {
break;
}
chs.remove(i);
}
}
private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value;
public TreeNode(int value) {
this.value = value;
}
public TreeNode() {
}
}
public static void main(String[] args) {
RecoverBinarySearchTree recoverBinarySearchTree = new RecoverBinarySearchTree();
char[] tree = new char[]{'3','4','5','#','#','2'};
List<Character> chars = new ArrayList<Character>();
recoverBinarySearchTree.binarySearchTreeToArray(recoverBinarySearchTree.recover(recoverBinarySearchTree.createTree(tree)), chars);
System.out.println(Arrays.toString(chars.toArray(new Character[chars.size()])));
chars = new ArrayList<Character>();
recoverBinarySearchTree.binarySearchTreeToArray(recoverBinarySearchTree.recoverTree(recoverBinarySearchTree.createTree(tree)), chars);
System.out.println(Arrays.toString(chars.toArray(new Character[chars.size()])));
}
}
leetcode — recover-binary-search-tree的更多相关文章
- 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 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [leetcode]Recover Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary searc ...
- [Leetcode] Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- LeetCode: Recover Binary Search Tree [099]
[题目] Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without cha ...
- [Leetcode] Recover binary search tree 恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- LeetCode Recover Binary Search Tree——二查搜索树中两个节点错误
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- 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 ...
随机推荐
- linux gdb
linux gdb linux 测试代码 #include <stdio.h> #include <stdlib.h> static char buf[255]; static ...
- windows下编译SDL1.2
首先,官网下载开发库,我这里用的是tdm-gcc,因此下载mingw版的. 解压,写代码,编译…… 成功!...地出错了 这里提一下,编译命令是 g++ test.cpp -I include目录 ...
- c# asp.net mvc4 使用uploadify插件实现上传功能
[1]首先去官网下载插件:http://www.uploadify.com/download/ .ww我使用的是免费的,基于flash的版本.因为基于H5的版本需付费使用,然后使用该插件也就是做做毕设 ...
- Oracle在.sql文件中创建存储过程
创建存储过程的语法网上到处都有. 可我执行了半天都创建不成功. 最后,发现! 在最后加个 / 就可以了!!! 真坑啊 今天连续被Oracle坑了两次了. 最后,感谢这个人https://blog.cs ...
- DevExpress内 GridControl中复选框值问题
在DevExpress的 GridControl内的复选柜勾选后,界面看到是勾选状态,但对应的DataView的值仍未变,在以下事件内处理 在对应的DataView内的 CellValueChangi ...
- Java Web 获取客户端真实IP
Java Web 获取客户端真实IP 发生的场景:服务器端接收客户端请求的时候,一般需要进行签名验证,客户端IP限定等情况,在进行客户端IP限定的时候,需要首先获取该真实的IP.一般分为两种情况: 方 ...
- 你不知道的JS之 this 和对象原型(一)this 是什么
原文:你不知道的js系列 JavaScript 的 this 机制并没有那么复杂 为什么会有 this? 在如何使用 this 之前,我们要搞清楚一个问题,为什么要使用 this. 下面的代码尝试去 ...
- 格式化数据保留两位小数,输入格式为 :xxx,xx,,,,x,,(x为浮点数)
/** * 格式化字符串 */ static String dataFormat(String data){ String formatedData = ""; // 浮点数正则表 ...
- json格式 (JavaScipt Object Notation)
json格式 json语法规则: 01.对象表现形式 key:value 键值对 02.如果有多个数据,之间使用逗号隔开 k1:v1,k2:v2 03.把对象写在大括号中 var student={a ...
- SQL 查询当前时间
Mysql: select date_format(now(),'%Y-%m-%d'); Oracle: Oracle中如何获取系统当前时间进行语句的筛选是SQL语句的常见功能 获取系统当前时间dat ...