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 ...
随机推荐
- 【Codeforces】Gym 101156G Non-Attacking Queens 打表
题意 求$n\times n$的棋盘上放$3$个皇后使得互相不攻击的方案数 拓展是$m\times n$棋盘上放$k$皇后,暴力打表找到了公式 OEIS 代码 import java.math.Big ...
- 线程绑定CPU核-sched_setaffinity
CPU亲合力就是指在Linux系统中能够将一个或多个进程绑定到一个或多个处理器上运行. 一个进程的CPU亲合力掩码决定了该进程将在哪个或哪几个CPU上运行.在一个多处理器系统中,设置CPU亲合力的掩码 ...
- 《java编程思想》读后笔记:一,标签
标签 是后面跟有冒号的标识符,格式如下: label : java中通过break与continue关键词可以完成类似于跳转的操作,其实现机制便是标签. 虽然很少有人使用,但是其有自身的适用场景:多层 ...
- Python: scikit-image binary descriptor
这个用例说明 BRIEF binary description algorithm from skimage import data from skimage import transform as ...
- BZOJ_3671_[Noi2014]随机数生成器_set+贪心
BZOJ_3671_[Noi2014]随机数生成器_set Description Input 第1行包含5个整数,依次为 x_0,a,b,c,d ,描述小H采用的随机数生成算法所需的随机种子.第 ...
- Tensorflow知识点学习
1.TensorFlow中Tensor维度理解: (1)对于2维Tensor 0维对应列 1维对应行 (2)维度操作举例: 对于k维的,tf.reduce_sum(x, axis=k-1)的结果是对最 ...
- Hibernate区分不同对象的方法
1.关系数据库按主键区分不同记录. create table CUSTOMERS (ID int promary key not null, NAME varchar(15)); insert ...
- MVN 命令行
Maven依赖查询: http://mvnrepository.com/ Maven常用命令: 1. 创建Maven的普通java项目: mvn archetype:create - ...
- android学习点滴一:android环境的搭建
东一点西一点,很多时间都浪费了.是该系统性的做好自己的东西了. <android学习点滴一:android环境的搭建> [环境变量]变量名:JAVA_HOME变量值:C:\Java\jdk ...
- day10servlet编程
Servlet学习的大纲: . servlet概念及相关接口简介 . servet 执行过程 . servlet路径映射 . 缺省servlet --应用 . servlet生命周 ...