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 ...
随机推荐
- Markdown初使用
Markdown是一种纯文本格式的标记语言.通过简单的标记语法,它可以使普通文本内容具有一定的格式. 相比WYSIWYG编辑器 优点:1.因为是纯文本,所以只要支持Markdown的地方都能获得一样的 ...
- VS2017简单使用
1. 2.删除下面的文件 3.点击属性 4.改为否 不使用预编译头 万能头文件自己导入网上有教程
- zipkin
转:https://blog.csdn.net/liaokailin/article/details/52077620 zipkin为分布式链路调用监控系统,聚合各业务系统调用延迟数据,达到链路调用监 ...
- js拼接字符串后swiper不能动的解决方案
swiper的配置一定要放在拼接字符串之后,紧随其后,如果放在其他的位置,swiper是不识别HTML的.
- Android Studio 错误: 非法字符: '\ufeff'
右下角:选UTF-8 convert一下,再重新编译..不知道为什么,本来好像就是UTF-8好奇怪. 还看到一个方法但我没试过,放在这里万一下次又遇见了这样的问题呢 右下角将UTF-8 convert ...
- python Cookie Session 相关用法
Cookie一.前言1.http协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响 应情况直接影响,也不会直接影响后面的请 ...
- Winsock编程基础1
1.加载和释放Winsoke库 //所有Winsock函数都是从WS2_32.DLL导出,包含相应库文件#include <winsock2.h>#pragma comment(lib, ...
- 一个开源的,跨平台的.NET机器学习框架ML.NET
微软在Build 2018大会上推出的一款面向.NET开发人员的开源,跨平台机器学习框架ML.NET. ML.NET将允许.NET开发人员开发他们自己的模型,并将自定义ML集成到他们的应用程序中,而无 ...
- Dora.Interception,为.NET Core度身打造的AOP框架 [5]:轻松地实现与其他AOP框架的整合
这里所谓的与第三方AOP框架的整合不是说改变Dora.Interception现有的编程,而是恰好相反,即在不改变现有编程模式下采用第三方AOP框架或者自行实现的拦截机制.虽然我们默认提供基于IL E ...
- [Swift]LeetCode395. 至少有K个重复字符的最长子串 | Longest Substring with At Least K Repeating Characters
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...