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

详见: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. Tips:PowerDesigner16.5 图表显示Code以及 Columns新增Commet显示

  2. struts2的结果类型

    1.从struts-default.xml入手,得到结果类型列表以及对应的处理类: <result-types> <!-- 转发到action --> <result-t ...

  3. bzoj 4453 cys就是要拿英魂! —— 后缀数组+单调栈+set

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4453 这种问题...一般先把询问离线,排序: 区间对后缀排名的影响在于一些排名大而位置靠后的 ...

  4. Java多线程加强

    一.传统多线程 public void start() Causes this thread to begin execution; the Java Virtual Machine calls th ...

  5. C# 性能分析工具

    http://msdn.microsoft.com/zh-cn/vstudio/aa497289(en-us).aspx Performance This section includes infor ...

  6. thinkpad取消fn键功能

    转自:https://bbs.thinkpad.com/thread-1834235-1-1.html 1就是一直觉得fn建自动开启很烦人,于是百度后得到 我们可以 控制面板 -- 键盘--think ...

  7. 网络爬虫之Xpath用法汇总

    众所周知,在设计爬虫时,最麻烦的一步就是对网页元素进行分析,目前流行的网页元素获取的工具有BeautifulSoup,lxml等,而据我使用的体验而言,Scrapy的元素选择器Xpath(结合正则表达 ...

  8. 第一个PyQuery小demo

    1.打开网址https://www.v2ex.com/,查看其源码. 2.打开PyCharm编译器,新建工程c3-11,新建python file,命名为v2ex.py,同时,新建file,命名为v2 ...

  9. Spring Boot错误errMsg: "request:ok"

    在把评论写到数据库并且动态刷新评论区的时候,有时候正常写入,有时候就会有“request:ok”的的错误出现,错误信息如下: data: {timestamp: , error: "Inte ...

  10. 有关Linux的.a、.so和.o文件---mark一下(转)

    gcc 生成 .a静态库和 .so动态库   (转载) 我们通常把一些公用函数制作成函数库,供其它程序使用.函数库分为静态库和动态库两种.静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该 ...