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 keysless thanthe node's key.
- The right subtree of a node contains only nodes with keysgreater thanthe 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.
这道题就一个难点:要会设置节点的两边限制值。
吸收了LeetCode论坛上的建议,不使用INT_MAX 和INT_MIN,我这里使用了LLONG_MIN和LLONG_MAX。
因为如果是用INT_MIN,那么第一个左子树的值为INT_MIN的时候就会判断为假,其实为真。
虽然leetcode没有测试这个情况,不过健全的程序总是最好的。
class Solution {
public:
bool isValidBST(TreeNode *root)
{
return validBST(root);
}
//注意:别忘记了两边的boundary:leftMax和rightMax的设置
/*
I dont think it's a good idea to use int to represent the up and low bound of a TreeNode, INT_MIN and INT_MAX maybe used by TreeNode. We can use double or just the TreeNode itself.
*/
bool validBST(TreeNode *root, long long leftMax = LLONG_MIN, long long rightMax = LLONG_MAX)
{
if (!root) return true;
if (!root->left && !root->right) return true;
if (root->left
&& (root->left->val >= root->val || root->left->val <= leftMax))
return false;
if (root->right
&& (root->right->val <= root->val || root->right->val >= rightMax ))
return false;
return validBST(root->left, leftMax, root->val)
&& validBST(root->right, root->val, rightMax);
}
};
//2014-2-15 update
bool isValidBST(TreeNode *root)
{
return isLegal(root);
}
bool isLegal(TreeNode *r, TreeNode *le = NULL, TreeNode *ri = NULL)
{
if (!r) return true;//||!r->left && !r->right不能增加这个条件
if (le && le->val >= r->val || ri && ri->val <= r->val) return false;
return isLegal(r->left, le, r) && isLegal(r->right, r, ri);
}
分享到:
相关推荐
LeetCode-BinarySearch
leetcode-tag-Tree
leetcode卡leetcode 二叉树卡片 LeetCode 二叉树卡片问题的章节智解
作者: 负雪明烛个人博客: :
BinaryTree.py是一个方便的工具,它可以构建和显示编码时所需的二叉树 演示 您需要在使用之前导入该类 import BinaryTree as bt 从值/对象列表或二叉树构造二叉树 t1 = bt.BinaryTree([1,2,3,4,5,'#',6,7,'#','#','#...
最大公共字符串leetcode 二叉树 二叉树是一种抽象的数据结构,由根节点和左右子树组成。 一个节点可以有零个、一个或两个子节点。 二叉树的类型 目标 能够熟悉二叉树上的各种术语。 能够实现二叉树节点。 能够使用...
leetcode卡除非您已经使用过卡片,否则不要看这里。 做真实的自己!
leetcode的题目:Balanced Binary Tree
Pow(xn) leetcode Binary-Search-3 问题1 Pow(x,n) () 问题2 找到 K 个最近的元素 ()
Binary-Search-1 问题1 搜索二维矩阵() 问题1 在旋转排序数组中搜索 () 问题2 在无限排序数组中搜索: 给定一个未知长度的排序数组和一个要搜索的数字,返回该数字在数组中的索引。 越界访问元素会引发异常。 如果该...
leetcode110 二叉树_高度平衡 力扣110
Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...
Validate Binary Search Tree - Java Recursive - Java Iterative - Java Inorder 0099 Recover Binary Search Tree - Java Recursive 0101 Symmetric tree - Java Recursive - Java Iterative - C Recursive...
Programming Questions on BinarySearch, LeetCode, CodeChef
* [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...
leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡...
704.Binary_Search二分查找【LeetCode单题讲解系列】
leetcode 数据结构题目中的答案,已经调试,直接运行,求二叉树的最小深度
leetcode 2 二叉树打印机 在极小的区域打印二叉树。...>binary-tree-printer</ artifactId > < version >1.0.0</ version > </ dependency > 例子 打印随机 BST。 BTPrinter . printRandom
二分查找Binary_Search套路和解题模板【LeetCode刷题套路教程3】