转载自:StudyCoder
题目:验证二叉搜索树
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.
Example 1:
2 / \ 1 3Binary tree
[2,1,3]
, return true.
Example 2:
1 / \ 2 3Binary tree
[1,2,3]
, return false.
题意:
给定一棵二叉树,检测该二叉树是否是一棵有效的二叉搜索树(BST)。
假定一棵二叉搜索树被如下定义:
1、节点的左子树所有节点的值小于该节点的值;
2、节点的右子树所有节点的值大于该节点的值;
3、左右子树本身也要是二叉搜索树;
转载地址:http://www.cnblogs.com/grandyang/p/4298435.html
思路一:
利用它本身的性质来做,即左<根<右,初始化时带入系统最大值和最小值,在递归过程中换成它们自己的节点值,用long代替int就是为了包括int的边界条件。
代码:C++版:18ms
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isValidBST(TreeNode* root) { return isValidBST(root, LONG_MIN, LONG_MAX); } bool isValidBST(TreeNode *root, long mn, long mx) { if (!root) return true; if (root->val <= mn || root->val >= mx) return false; return isValidBST(root->left, mn, root->val) && isValidBST(root->right, root->val, mx); } };
思路二:
这题实际上简化了难度,因为一般的二叉搜索树是左<=根<右,而这道题设定为左<根<右,那么就可以用中序遍历来做。因为如果不去掉左=根这个条件的话,那么下边两个数用中序遍历无法区分:
20 20
/ \
20 20
它们的中序遍历结果都一样,但是左边的是BST,右边的不是BST。去掉等号的条件则相当于去掉了这种限制条件。下面我们来看使用中序遍历来做,这种方法思路很直接,通过中序遍历将所有的节点值存到一个数组里,然后再来判断这个数组是不是有序的。
代码:C++版:16ms
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isValidBST(TreeNode* root) { if (!root) return true; vector<int> vals; inorder(root, vals); for (int i=0; i<vals.size()-1; ++i) { //判断数组是否满足从大到小的顺序,从而验证该树是否是有效的二叉搜索树 if (vals[i] >= vals[i+1]) return false; } return true; } void inorder(TreeNode *root, vector<int> &vals) { //中序遍历递归 if (!root) return; inorder(root->left, vals); vals.push_back(root->val); inorder(root->right, vals); } };
思路三:
这种解法跟上面那个很类似,都是用递归的中序遍历,但不同之处是不将遍历结果存入一个数组遍历完成再比较,而是每当遍历到一个新节点时和其上一个节点比较,如果不大于上一个节点那么则返回false,全部遍历完成后返回true。
代码:C++版:16ms
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode *pre; bool isValidBST(TreeNode* root) { int res = 1; pre = NULL; inorder(root, res); if (res == 1) return true; else return false; } void inorder(TreeNode *root, int &res) { if (!root) return; inorder(root->left, res); if (!pre) { pre = root; } else { if (root->val <= pre->val) res = 0; pre = root; } inorder(root->right, res); } };
思路四:
也可以用非递归来做,需要用到栈,因为中序遍历可以非递归来实现,所以只要在其上面稍加改动便可。以上除了思路一之外均是借助二叉树的中序遍历改进而来,所以理论上,中序遍历的所有解法也都能解决本题。如中序遍历中的Morris遍历算法,相对于其他解法而言相对复杂,但是算法的空间复杂度只有O(1)。
代码:C++版:16ms
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isValidBST(TreeNode* root) { if (!root) return true; stack<TreeNode*> stk; TreeNode *p = root; TreeNode *pre = NULL; while (p || !stk.empty()) { while (p) { stk.push(p); p = p->left; } p = stk.top(); stk.pop(); if (!pre) { pre = p; } else { if (p->val <= pre->val) return false; } pre = p; p = p->right; } return true; } };