two fork Find tree
Given a binary tree, determine if it is a valid two-fork search tree (BST)
A BST is defined as:
The value in the left subtree of the node is strictly less than the value of that node.
The value in the right subtree of the node is strictly greater than the value of that node.
The left and right subtree must also be a two-fork search tree.
Because the middle sequence traversal of the binary lookup tree is ordered. So verifying whether it is a two-fork lookup tree, traversing the binary tree in the middle order, if the value of the previous node is greater than the value of the current node, proves that this is not a binary tree.
Code Implementation
BOOLIsvalidbst (TreeNode *root) {//Write your code here if(Root = NULL)return true; Stack<TreeNode*>Stk TreeNode *pre = NULL; while(Root | |!stk.empty ()) {if(Root) {Stk.push (root); root = root->left; }Else{root = Stk.top (); Stk.pop ();if(Pre && (root->val <= pre->val))return false; Pre = root; root = root->right; } }return true; }
(Validate) Binary Search Tree
http://www.lintcode.com/zh-cn/problem/validate-binary-search-tree/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Verify Binary Lookup tree