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的更多相关文章

  1. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

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

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

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

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

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

  7. [leetcode]Validate Binary Search Tree @ Python

    原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/ 题意:检测一颗二叉树是否是二叉查找树. 解题思路:看到二叉树我们首 ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  10. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

随机推荐

  1. 无法运行 vue-manage-system@3.1.0 dev: `webpack-dev-server --inline --progress --

    一个项目的变大好多人开发,难免会有很多的冲突.每次跟新代码都要一个坑一个坑的解决的.这次遇到这个坑好大.急死了.... 百度了好多说占用端口,试了好几遍不行.最终还是要去查原因的....经过了几个小时 ...

  2. 在浏览器输入URL时发生了什么

    浏览器器检查cache,如果请求对象已经缓存并且是最新的,执行第9步. 浏览器询问操作系统,请求服务器的IP地址 操作系统进行DNS查找,然后告诉浏览器服务器的IP 浏览器和服务器简历一个TCP连接( ...

  3. MVCAPi Httpclient

    APi配制文件 删除修改api 显示和命名空间 新增

  4. node.js 递归复制文件夹(附带文件过滤功能)

    1.简介: 很简单,写了一个node操作文件的小脚本,主要实现对目标文件夹中内容的复制.还顺带一个按照文件夹或者文件名过滤的功能. 2.应用场景 适合基于 node 环境的项目,项目打包的时候,配合 ...

  5. 2019_BUAAOO_第二单元总结

    第一次作业:单部多线程傻瓜调度电梯 设计策略 本次作业我才用的是生产者消费者模式,创建一个RequestList类,将输入线程InputThread作为生产者,负责将请求放入RequestList:将 ...

  6. 【数据结构】红黑树与跳表-(SortSet)-(TreeMap)-(TreeSet)

    SortSet 有序的Set,其实在Java中TreeSet是SortSet的唯一实现类,内部通过TreeMap实现的:而TreeMap是通过红黑树实现的:而在Redis中是通过跳表实现的: Skip ...

  7. javaweb聊天室源码免费

    效果:可擴展源碼,免費技術指導,電話13956301647 链接:https://pan.baidu.com/s/1FWV8DNWacGaEpAQEsWsNaw 提取码:b6gf

  8. d3.js做的柱状图

    window.onload = function(){ var dataArray = [23, 13, 21, 14, 37, 15, 18, 34, 30]; var height = 400,w ...

  9. 如何在JSP中获得Cookie对象

    Cookie cookies[]=request.getCookies(); //读出用户硬盘上的Cookie,并将所有的Cookie放到一个cookie对象数组里面 Cookie sCookie=n ...

  10. 脚本语言丨Batch入门教程第三章:逻辑判断

    通过学习Batch入门教程的前两章内容,我们已经大致掌握了基本概念和认识变量的相关内容,今天我们要跟大家继续分享第三章内容:Batch入门教程之逻辑判断.  前期回顾  ◀Batch入门教程丨部署与H ...