Advertisement
i_love_rao_khushboo

Untitled

Sep 3rd, 2022 (edited)
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. bool is_leaf(TreeNode* root) {
  2.     if(root == NULL) return 0;
  3.     return (root->left == NULL) and (root->right == NULL);
  4. }
  5.  
  6. // We are considering that BSTs can not contain duplicate Nodes.
  7. bool check_bst(TreeNode *root) {
  8.     if((root == NULL) or (is_leaf(root) == 1)) return 1;
  9.    
  10.     bool res = 1;
  11.     if(root->left) res &= (root->left->val < root->val);
  12.     if(root->right) res &= (root->right->val > root->val);
  13.    
  14.     return (res and check_bst(root->left) and check_bst(root->right));
  15. }
  16.  
  17. /* One possible tree where the above algorithm will give wrong result is :--->
  18.  
  19.            5
  20.           / \
  21.          /   \
  22.         4     6
  23.              / \
  24.             /   \
  25.            3     7
  26. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement