099 Recover Binary Search Tree 复原二叉搜索树
二叉排序树中有两个节点被交换了,要求把树恢复成二叉排序树。
详见:https://leetcode.com/problems/recover-binary-search-tree/submissions/
Java实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void recoverTree(TreeNode root) {
List<TreeNode> node=new ArrayList<TreeNode>();
List<Integer> val=new ArrayList<Integer>();
inOrder(root,node,val);
Collections.sort(val);
for(int i=0;i<node.size();++i){
if(node.get(i).val!=val.get(i)){
node.get(i).val=val.get(i);
}
}
}
private void inOrder(TreeNode root,List<TreeNode> node,List<Integer> val){
if(root==null){
return;
}
inOrder(root.left,node,val);
node.add(root);
val.add(root.val);
inOrder(root.right,node,val);
}
}
099 Recover Binary Search Tree 复原二叉搜索树的更多相关文章
- [LeetCode] 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 ...
- [leetcode]99. 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 ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] 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 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- win装wamp
前传: 刚换了工作环境,从原来的全mac转到全windows,很不适应,简单的wamp鼓捣了半天 环境: win 7 旗舰版 ,wamp ,下载地址: http://pan.baidu.com/s/1 ...
- 局域网扫描IP
今天有朋友去面试,被问到一个“如何扫描局域网IP”的问题(即找出局域网中当前已使用的IP),朋友回答的不好,回来问我,我首先想到的就是使用ping命令将局域网可分配的IP地址逐个遍历一遍,能ping通 ...
- Nginx均衡负载配置
前言:Nginx也是一种服务器,反向代理服务器.单一tomcat能承受的并发访问量在150-200之间,还是在比较理想的情况下,当并发量超出这个范围,便需要Nginx实现多个tomcat的均衡负载,但 ...
- 苹果手机app试玩赚钱平台汇总
注意: 微信扫码下载,绑定手机号和微信.才能提现 每天3点更新任务,4点最多! | 平台 | 提现额 | 任务量| 推荐强度 | 扫码 | 1.小鱼,10元,大量,强推! →点开扫码 2.天天飞燕,5 ...
- 关于在linux python源文件头部添加 “#!/usr/bin/env python” 不能直接运行的问题
如果环境变量设置正确 如果文件是从windows拷贝到linux中的 可能是换行符造成的.试试dos2unix命令,或相似的命令,把dos格式的换行符转为unix格式.
- django 多数据库配置
在django项目中, 一个工程中存在多个APP应用很常见. 有时候希望不同的APP连接不同的数据库,这个时候需要建立多个数据库连接. 1. 修改项目的 settings 配置 在 settings. ...
- The Tomcat server configuration at \Servers\Tomcat v8.0 Server at localhost-config is missing. Check the server for erro
解决方案 1.选择Eclipse工具栏中的Windows→Perferences 2.remove已经创建的server 3.选择Add重新添加,选择create anew local server ...
- 启用数据库 aspnetstate 会话状态
http://www.cnblogs.com/klzwj1988/archive/2010/05/10/1731723.html
- ENFP喜欢的职业
外向(E)+直觉(N)+情感(F)+知觉(P). 1. 设计:设计本身很能满足ENFP对工作的各种要求,但是有个附加条件就是,这份工作不能让ENFP长时间的一个人工作,没机会和别人交流,也就是说有一个 ...
- Linux下搭建tomcat集群全记录
(转) Linux下搭建tomcat集群全记录 2011-10-12 10:23 6133人阅读 评论(1) 收藏 举报 tomcatlinuxapacheinterceptorsession集群 1 ...