leetcode:Validate Binary Search Tree
来源:互联网 发布:迷羊 郁达夫 知乎 编辑:程序博客网 时间:2024/06/16 21:36
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.
void preorder(TreeNode* root, vector<int>& ret){if (root == NULL)return;preorder(root->left, ret);ret.push_back(root->val);preorder(root->right, ret);}bool isValidBST(TreeNode *root) {if (root == NULL)return true;vector<int> ret;preorder(root, ret);for (size_t i = 1; i < ret.size(); ++i){if (ret[i] <= ret[i - 1])return false;}return true;}
(2)递归判断,代码如下:
bool help(TreeNode* root, long long leftVal, long long rightVal){if (root == NULL)return true;return (leftVal < root->val && root->val < rightVal && help(root->left, leftVal, root->val) && help(root->right, root->val, rightVal));}bool isValidBST(TreeNode *root) {return help(root, LLONG_MIN, LLONG_MAX);}
0 0
- 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
- iOS地图上WGS84、GCJ-02、BD-09互转解决方案
- 解析如何搭建Struts2+Hibernate4+Spring4框架
- 关于EditText的属性和常识
- 反转字符串
- U盘系统Fbinst&Grldr菜单命令详解
- leetcode:Validate Binary Search Tree
- can only be used when targeting iOS 4.2 or later
- Java虚拟机(七)-JVM内存模型总结
- maven同乐所用命令
- BAT及各大互联网公司2014前端笔试面试题:HTML/CSS篇
- java操作mongodb(分页)
- IE11 Windows7下F12 DOC资源管理器不能用Exception in window.onload: Error: An error has ocurredJSPlugin.3005
- 禁用調用函數HalLcdWriteString後向串口輸出調試信息
- MSVCRTD.lib(crtexe.obj) : error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStart