Java for LeetCode 099 Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
解题思路:
先中序遍历找到mistake,然后替换即可,JAVA实现如下:
public void recoverTree(TreeNode root) {
List<Integer> list = inorderTraversal(root);
int left = 0, right = 0;
boolean first = true;
for (int i = 0; i < list.size(); i++) {
if (first && i != list.size() - 1 && list.get(i) >= list.get(i + 1)) {
left = list.get(i);
first = false;
}
if (!first && i != 0 && list.get(i) < list.get(i - 1)) {
right = list.get(i);
}
}
transformTreeNode(root, right, Integer.MAX_VALUE);
transformTreeNode(root, left, right);
transformTreeNode(root, right, left); } public static void transformTreeNode(TreeNode root, int souce, int target) {
if (root == null)
return;
if (root.val == souce) {
root.val = target;
return;
}
transformTreeNode(root.left, souce, target);
transformTreeNode(root.right, souce, target);
} static public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
if (root == null)
return list;
if (root.left != null)
list.addAll(inorderTraversal(root.left));
list.add(root.val);
if (root.right != null)
list.addAll(inorderTraversal(root.right));
return list;
}
Java for LeetCode 099 Recover Binary Search Tree的更多相关文章
- [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
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- leetcode 99 Recover Binary Search Tree ----- java
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Java for LeetCode 098 Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [leetcode]99. Recover Binary Search Tree恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 099 Recover Binary Search Tree 复原二叉搜索树
二叉排序树中有两个节点被交换了,要求把树恢复成二叉排序树. 详见:https://leetcode.com/problems/recover-binary-search-tree/submission ...
- Leetcode#99 Recover Binary Search Tree
原题地址 中序遍历二叉搜索树,正常情况下所有元素都应该按递增排列,如果有元素被交换,则会出现前面元素大于后面的情况,称作反序.由于交换了两个节点,所以通常会有两处反序,但如果是两个相邻节点发生了交换, ...
- 第五周 Leetcode 99. Recover Binary Search Tree (HARD)
Leetcode99 给定一个 二叉搜索树,其中两个节点被交换,写一个程序恢复这颗BST. 只想到了时间复杂度O(n)空间复杂度O(h) h为树高的解法,还没想到空间O(1)的解法. 交换的情况只有两 ...
随机推荐
- andriod GridLayout
来自:http://blog.csdn.net/jianghuiquan/article/details/8299973 GridLayout网格布局 android4.0以上版本出现的GridLay ...
- 15 THINGS ALL GIRLS SHOULD KNOW ABOUT THEIR VAGINA
Here are 15 facts that EVERY GIRL should know about her vagina. Don’t be shy! Your vagina is part of ...
- DotnetBrowser入门教程-入门
在.net core时代,web开发基本可以用.net core 2.0取代了.但是在传统领域,桌面开发仍然是不可以抛弃的,譬如: 1.用户需要和串口或者硬件打交道. 2.用户只想简单的安装好就使用, ...
- java中String与equals,==详解
首先,String str1="abc",这个str1所指向的是常量池中的一块内存. 如果又有,String str2="abc",那么str1和str2所指向 ...
- 2017.2.28 activiti实战--第五章--用户与组及部署管理(二)部署流程资源
学习资料:<Activiti实战> 第五章 用户与组及部署管理(二)部署流程资源 内容概览:讲解流程资源的读取与部署. 5.2 部署流程资源 5.2.1 流程资源 流程资源常用的有以下几种 ...
- 全国车辆违章查询API文档及demo
简介 聚合数据全国车辆违章API,目前已经支持300个左右的城市违章查询,已连接上万个APP.方便有车一族随时了解自己是否有过交通违章,避免因遗忘或逾期处理违章罚单而造成的不必要损失. API参考文档 ...
- Easy UI form表单提交 IE浏览器不执行success ,以及 datagrid 展示过慢
最近在做一个Easy ui的项目 发现了一些问题,在这里总结下 1.表单提交,后端代码 public ActionResult Save(Request model) { ResultInfo _in ...
- hdu5386(暴力)
题意: 给出两个n*n的矩阵,一个作为初始矩阵.一个作为目标矩阵,给出m个操作,操作有两种,一种是"L,x,y".代表我们要把x这一行赋成y,还有一种是"H,x,y&qu ...
- versions 忽略 xcuserdata 目录
1.打开versions,选中xcuserdata目录 2.菜单条.Action->ignore "..." 3.versions不再显示不同
- android中使用百度定位sdk实时的计算移动距离
; //5秒刷新一次 private Handler refreshHandler = new Handler(){ //刷新界面的Handler public void handleMessag ...