ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【二叉搜索树BST】98. Validate Binary Search Tree

2020-10-01 14:00:56  阅读:187  来源: 互联网

标签:Binary Search right TreeNode min BST max root left


问题:

判断一颗二叉树是否为二叉搜索树。

 

解法:Binary Tree(二叉树)

 

首先,了解 二叉搜索树BST的定义:

A binary search tree is a rooted binary tree whose internal nodes each store a key greater than all the keys in the node's left subtree and less than those in its right subtree.

-> left < root < right

e.g. 下图中,2 < 5 < 6

 

求给定函数  bool isValidBST(TreeNode* root)

而本题目中,例如节点4,不能只判断4只要>2就好,同时4还得<5,这就不止一个变量(当前节点root)能完全覆盖。

每次递归中由3个随递归调用变化的变量:

当前节点root,当前节点的最小值min(开区间,不取到),当前节点的最大值max(开区间,不取到)。

因此,我们追加辅助递归函数,bool isValidBSThelp(TreeNode* root, TreeNode* min, TreeNode* max)

  • DEF
    • 判断:当前节点root是否有 [ min < root < max ],即 (min, max)
      • 若没有,即 [ min >= root or root >= max ],则返回false。
      • 否则,本层root没问题,继续递归判断 root->left 和 root->right。这时判断范围:
        • root->left: (min, root)
        • root->right: (root, max)
  • STAT
    • 当前节点 root
    • 开区间最小值 min
    • 开区间最大值 max
  • OPT
    • 得到递归结果left和right后,返回
      • left && right
  • BASE
    • root==null,return true
    • min!=null,if [min >= root] return false
    • max!=null, if [root >= max] return false

 

代码参考:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 8  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 9  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10  * };
11  */
12 class Solution {
13 public:
14     //DEF:
15     //   to find if: [min < root < max]
16     // if [min>=root or root>=max] then return false
17     // else this level is good, then check the next level recursionly:
18     //      to find if [min < root->left < root] && [root < root->right < max]
19     //STAT: root, min, max
20     //OPT: after got the result as left, right.
21     //     return left && right
22     //BASE:
23     //    root==null, return true
24     //    min!=null->if [min>=root], return false
25     //    max!=null->if [root>=max], return false
26     bool isValidBSThelp(TreeNode* root, TreeNode* min, TreeNode* max) {
27         if(!root) return true;
28         if(min && min->val >= root->val) return false;
29         if(max && max->val <= root->val) return false;
30         
31         return isValidBSThelp(root->left, min, root) &&
32             isValidBSThelp(root->right, root, max);
33     }
34     bool isValidBST(TreeNode* root) {
35         return isValidBSThelp(root, nullptr, nullptr);
36     }
37 };

 

标签:Binary,Search,right,TreeNode,min,BST,max,root,left
来源: https://www.cnblogs.com/habibah-chang/p/13757782.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有