Validate Binary Search Tree -- LeetCode
2016-01-27 06:36
531 查看
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.
思路:二叉搜索树如果按照in-order traversal来遍历的话,所有节点会按照从小到大的顺序被遍历。
Pre-order
Display the data part of the root (or current node).
Traverse the left subtree by recursively calling the pre-order function.
Traverse the right subtree by recursively calling the pre-order function.
In-order
Traverse the left subtree by recursively calling the in-order function.
Display the data part of the root (or current node).
Traverse the right subtree by recursively calling the in-order function.
In a BST, in-order traversal retrieves data in sorted order.
Post-order
Traverse the left subtree by recursively calling the post-order function.
Traverse the right subtree by recursively calling the post-order function.
Display the data part of the root (or current node).
代码中,我用了一个全局指针pre来记录in-order traversal过程中的上一个节点。如果上一个节点的值比当前节点的值要大或者相等,则不是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.
思路:二叉搜索树如果按照in-order traversal来遍历的话,所有节点会按照从小到大的顺序被遍历。
Pre-order
Display the data part of the root (or current node).Traverse the left subtree by recursively calling the pre-order function.
Traverse the right subtree by recursively calling the pre-order function.
In-order
Traverse the left subtree by recursively calling the in-order function.Display the data part of the root (or current node).
Traverse the right subtree by recursively calling the in-order function.
In a BST, in-order traversal retrieves data in sorted order.
Post-order
Traverse the left subtree by recursively calling the post-order function.Traverse the right subtree by recursively calling the post-order function.
Display the data part of the root (or current node).
class Solution { public: bool validate(TreeNode* root, TreeNode* &pre) { if (root == NULL) return true; if (!validate(root->left, pre)) return false; if (pre != NULL && pre->val >= root->val) return false; pre = root; return validate(root->right, pre); } bool isValidBST(TreeNode* root) { TreeNode* pre = NULL; return validate(root, pre); } };
代码中,我用了一个全局指针pre来记录in-order traversal过程中的上一个节点。如果上一个节点的值比当前节点的值要大或者相等,则不是BST。
相关文章推荐
- polymorphism
- Binary Tree Level Order Traversal -- LeetCode
- LeetCode "Count of Range Sum" !!
- Java 线程的状态
- 9. Palindrome Number
- 原来只是在原地打转
- 多层TreeWidget可选实现
- 开源的Delphi性能调试工具
- 最受华为用户欢迎的五个实用工具软件(华为文件管理)
- Fantope Projection and Selection: A near-optimal convex relaxation of sparse PCA
- 夺命雷公狗---微信开发08----发送歌名,在线听取MP3
- 软件开发管理:源码版本管理
- 夺命雷公狗---微信开发07----接收地理位置消息,并用文本方式返回
- 7. Reverse Integer
- 小娜学法(18):自动做案情摘要
- linux 解压命令大全[转]
- JDBC详解
- Leetcode 46 - Permutations
- Ubuntu中应用程序添加快速启动器
- 关于Error: Failed to lookup view "error" in views directory "D:\WebProgram\webBlog\views"的解决方法