leetcode — validate-binary-search-tree
import apple.laf.JRSUIUtils;
/**
* Source : https://oj.leetcode.com/problems/validate-binary-search-tree/
*
*
*
* Given a binary tree, determine if it is a valid binary search tree (BST).
*
* Assume a BST is defined as follows:
*
* The left subtree of a node contains only nodes with keys less than the node's key.
* The right subtree of a node contains only nodes with keys greater than the node's key.
* Both the left and right subtrees must also be binary search trees.
*
* confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
*
* OJ's Binary Tree Serialization:
*
* The serialization of a binary tree follows a level order traversal, where '#' signifies
* a path terminator where no node exists below.
*
* Here's an example:
*
* 1
* / \
* 2 3
* /
* 4
* \
* 5
*
* The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
*/
public class ValidateBinarySearchTree {
public boolean validate (TreeNode root) {
return isValid(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
/**
* 先判断根节点是否满足 root.value > min && root.value < max,如果满足,再递归判断左右子树
*
* @param root
* @param min
* @param max
* @return
*/
public boolean isValid (TreeNode root, int min, int max) {
if (root == null) {
return true;
}
if (root.value > max || root.value < min) {
return false;
}
return isValid(root.leftChild, min, root.value) && isValid(root.rightChild, root.value, max);
}
/**
* 二叉搜索树的中序遍历结果是单调递增的,所以中序遍历的时候当前节点值大于上一个节点的值
* 注意:这里每次递归会改变lastMax的值,需要保存下来,所以这里需要一个类似指针的变量,不能直接使用Integer等包装类型
*
* @param root
* @param lastMax
* @return
*/
public boolean isValidByInorder (TreeNode root, TreeNode lastMax) {
if (root == null) {
return true;
}
if (!isValidByInorder(root.leftChild, lastMax)) {
return false;
}
if (lastMax.value >= root.value) {
return false;
}
lastMax.value = root.value;
return isValidByInorder(root.rightChild, lastMax);
}
public boolean validate1 (TreeNode root) {
TreeNode lastMax = new TreeNode(Integer.MIN_VALUE);
return isValidByInorder(root, lastMax);
}
public TreeNode createTree (char[] treeArr) {
TreeNode[] tree = new TreeNode[treeArr.length];
for (int i = 0; i < treeArr.length; i++) {
if (treeArr[i] == '#') {
tree[i] = null;
continue;
}
tree[i] = new TreeNode(treeArr[i]-'0');
}
int pos = 0;
for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
if (tree[i] != null) {
tree[i].leftChild = tree[++pos];
if (pos < treeArr.length-1) {
tree[i].rightChild = tree[++pos];
}
}
}
return tree[0];
}
private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value;
public TreeNode(int value) {
this.value = value;
}
public TreeNode() {
}
}
public static void main(String[] args) {
ValidateBinarySearchTree validateBinarySearchTree = new ValidateBinarySearchTree();
System.out.println(validateBinarySearchTree.validate(validateBinarySearchTree.createTree(new char[]{'1','2','3','#','#','4','#','#','5'})) + "-------false");
System.out.println(validateBinarySearchTree.validate(validateBinarySearchTree.createTree(new char[]{'3','1','4'})) + "-------true");
System.out.println(validateBinarySearchTree.validate(validateBinarySearchTree.createTree(new char[]{'3','2','4','1','#'})) + "-------true");
System.out.println();
System.out.println(validateBinarySearchTree.validate1(validateBinarySearchTree.createTree(new char[]{'1','2','3','#','#','4','#','#','5'})) + "-------false");
System.out.println(validateBinarySearchTree.validate1(validateBinarySearchTree.createTree(new char[]{'3','1','4'})) + "-------true");
System.out.println(validateBinarySearchTree.validate1(validateBinarySearchTree.createTree(new char[]{'3','2','4','1','#'})) + "-------true");
}
}
leetcode — validate-binary-search-tree的更多相关文章
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- [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] 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 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: Validate Binary Search Tree [098]
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- LeetCode :: Validate Binary Search Tree[具体分析]
Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less th ...
- [leetcode]Validate Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/ 题意:检测一颗二叉树是否是二叉查找树. 解题思路:看到二叉树我们首 ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- [GIT] 更新.repo目录
cd .repo/manifests/ git co -f git pull
- [LeetCode] Car Fleet 车队
N cars are going to the same destination along a one lane road. The destination is target miles awa ...
- 使用自建Git服务器管理私有项目 Centos 7.3 + Git 2.11.0 + gitosis (实测 笔记)
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7-x86_64-Minimal-1611.iso GIT服务器IP:192.168.1 ...
- EDI
EDI, Electronic Data Interchange,电子数据交换 EDI 商务是指将商业或行政事务按一个公认的标准,形成结构化的事务处理或文档数据格式,从计算机到计算机的电子传输方法.简 ...
- xgboost 多gpu支持 编译
xgboost 多gpu支持 编译 Ubuntu 18.04.2Linux 4.15.0-46-genericgcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0 cuda ...
- QT中foreach的使用
在标准C++中,并没有foreach关键字. 但是在QT中,可以使用这一个关键字,其主要原因是QT自己增加了这一个关键字,就像slots和signals.emit等一样.增加的foreach关键字在编 ...
- LVM学习笔记
LVM Logical Volume Manager Volume management creates a layer of abstraction over physical storage, a ...
- 【开源项目】电视盒子好用又强大的APP: TVRemoteIME
TVRemoteIME 电视盒子的远程输入法应用,可跨屏远程输入.跨屏远程控制盒子.远程文件管理.HTTP/RTMP/MMS网络视频直播.ED2K/种子文件的视频文件边下边播 应用的诞生 自从家里有电 ...
- Android OpenSL ES 开发:Android OpenSL 介绍和开发流程说明
一.Android OpenSL ES 介绍 OpenSL ES (Open Sound Library for Embedded Systems)是无授权费.跨平台.针对嵌入式系统精心优化的硬件音频 ...
- MyEclipse设置Console输出到文件
Java程序默认输出为Console,如果要想将Console输出结果保存到文件中,则需要做如下配置: 在JAVA程序上右键--> Run As --> Run Configuration ...