4.5 implements a function that checks whether a binary tree is a two-fork lookup tree.
Reference: http://blog.csdn.net/sgbfblog/article/details/7771096
C + + Implementation code:
#include <iostream>#include<New>#include<climits>using namespacestd;structtreenode{intVal; TreeNode*Left ; TreeNode*Right ; TreeNode (intx): Val (x), left (null), right (null) {}};voidInsert (TreeNode *&root,intkey) { if(root==NULL) Root=NewTreeNode (key); Else if(key<root->val) Insert (Root-Left,key); ElseInsert (Root-right,key);}voidCreatbst (TreeNode *&root) { intarr[Ten]= { in,4,6,1,8,3,0, +, at, the}; for(auto A:arr) insert (root,a);}BOOLIshelper (TreeNode *root,intMinintmax) { if(root==NULL)return true; if(root->val<=min| | root->val>=max)return false; returnIshelper (Root->left,min,root->val) &&ishelper (root->right,root->Val,max);}BOOLIsbst (TreeNode *root) { returnIshelper (Root,int_min,int_max);}voidInorder (TreeNode *root) { if(Root) {inorder (root-Left ); cout<<root->val<<" "; Inorder (Root-Right ); }}intMain () {TreeNode*root=NULL; Creatbst (root); Inorder (root); cout<<Endl; cout<<isbst (Root) <<Endl;}
careercup-Tree and Figure 4.5