Leetcode: Validate Binary Search Tree
来源:互联网 发布:手机电影软件 编辑:程序博客网 时间:2024/05/16 23:49
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.
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool islessthanroot(TreeNode* left, int val) { if(left==NULL)return true; if(left->val >= val)return false; return islessthanroot(left->left,val) && islessthanroot(left->right,val); } bool islargethanroot(TreeNode* right, int val) { if(right==NULL)return true; if(right->val <= val)return false; return islargethanroot(right->left,val) && islargethanroot(right->right,val); } bool isValidBST(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root == NULL) return true; if(islessthanroot(root->left,root->val) && islargethanroot(root->right,root->val)) { return isValidBST(root->left) && isValidBST(root->right); }else return false; }};
- LeetCode: Validate Binary Search Tree
- [Leetcode] Validate Binary Search Tree
- LeetCode : Validate Binary Search Tree
- [LeetCode] Validate Binary Search Tree
- [LeetCode] Validate binary search tree
- [LeetCode]Validate Binary Search Tree
- [Leetcode]Validate Binary Search Tree
- [leetcode]Validate Binary Search Tree
- LeetCode-Validate Binary Search Tree
- [leetcode] Validate Binary Search Tree
- LeetCode - Validate Binary Search Tree
- [LeetCode] Validate Binary Search Tree
- Leetcode Validate Binary Search Tree
- LeetCode | Validate Binary Search Tree
- LeetCode - Validate Binary Search Tree
- Leetcode: Validate Binary Search Tree
- 【Leetcode】Validate Binary Search Tree
- 【leetcode】Validate Binary Search Tree
- yii 增删改查
- Ant 构建错误 Specified VM install not found: type Standard VM, name jdk1.6. 最靠谱
- Huawei面试
- HTTP请求&&响应
- 关于“精益求精”的学习方法
- Leetcode: Validate Binary Search Tree
- hadoop的thriftserver配置
- 压缩容器多余容量
- 虚拟机VMware8.0安装Ubuntu12.04
- 椰树菜刀批量拿站详解 视频教程
- VTK资料收集
- jquery value值为某个数变成选中(selected)
- php.ini error_reporting设置,错误等级。
- c语言ODBC连接数据库