二叉排序树中有两个节点被交换了,要求把树恢复成二叉排序树。

详见: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 复原二叉搜索树的更多相关文章

  1. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  2. [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  3. [leetcode]99. Recover Binary Search Tree恢复二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  4. [Leetcode] Recover binary search tree 恢复二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  5. [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 ...

  6. [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 ...

  7. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. HihoCoder1651 : 小球染色([Offer收割]编程练习赛38)(DP的优化)

    描述 小Ho面前有N个小球排成了一排.每个小球可以被染成M种颜色之一. 为了增强视觉效果,小Ho希望不存在连续K个或者K个以上的小球颜色一样. 你能帮小Ho计算出一共有多少种不同的染色方法么? 例如N ...

  2. 2010浙大:zoj问题

    题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. 是否AC的规则如下:1. zoj能AC:2. 若字符串形式为xzojx,则也能AC,其中x可以是N个'o' 或者 ...

  3. 查看MySql数据库物理文件存放位置

    查找数据库文件位置使用命令 show global variables like "%datadir%";

  4. this在方法赋值过程中无法保持(隐式丢失)

    在看<高级程序设计>(我的红宝书) P.183页时遇到下面一个问题 var name = "77"; var obj = { name: "88", ...

  5. DTP模型之一:(XA协议之三)MySQL数据库分布式事务XA优缺点与改进方案

    1 MySQL 外部XA分析 1.1 作用分析 MySQL数据库外部XA可以用在分布式数据库代理层,实现对MySQL数据库的分布式事务支持,例如开源的代理工具:ameoba[4],网易的DDB,淘宝的 ...

  6. asp.net mvc Model验证总结及常用正则表达式【转载】

    关于Model验证官方资料: http://msdn.microsoft.com/zh-cn/library/system.componentmodel.dataannotations.aspx AS ...

  7. [hdu1269]迷宫城堡(SCC)

    题意:求一张图的强连通分量,若为1,则输出yes,否则输出no. 解题关键:targin算法模板题. #include<iostream> #include<cstring> ...

  8. SpringBoot @RequestBody 中文乱码

    今天突然想学习一下Restful风,详细的我就不赘述了,我的理解是同一个请求路径根据请求方式不同进行不同的处理 如四种提交方式,这里推荐一个插件Postman,可以模仿各种请求类型,自行百度安装吧 G ...

  9. httpservlet的service()、doget()、dopost方法

    HttpServlet里的三个方法:service(HttpServletRequest req, HttpServletResponse resp) ,doGet(HttpServletReques ...

  10. # program once 用途 及与 ifndef使用异同

    在头文件中用这种写法就是为了该头文件被重复包含时不会出现符合重定义的错误. 效果等同于     #ifndef __xxx__     #define __xxx__     ...    #endi ...