zoukankan      html  css  js  c++  java
  • Validate Binary Search Tree

    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. 

    confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

     

    Analyse: 

    1. Using upper bound(INT_MAX) and lower bound(INT_MIN).

    NA: Because if the root is INT_MAX / INT_MIN, it will cause error.

    View Code

    There is a chancy method: we can set the upper bound higher and the lower bound smaller. And let the arguments be long long int type.

    Runtime: 12ms.

    View Code

    2. Use inorder traversal to get the inorder sequence, then check whether it's ascending. 

      Runtime: 16ms.

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     bool isValidBST(TreeNode* root) {
    13         if(!root) return true;
    14         
    15         vector<int> result;
    16         inorder(root, result);
    17         for(int i = 1; i < result.size(); i++){
    18             if(result[i] <= result[i - 1]) return false;
    19         }
    20         return true;
    21     }
    22     void inorder(TreeNode* root, vector<int>& result){
    23         if(root->left) inorder(root->left, result);
    24         result.push_back(root->val);
    25         if(root->right) inorder(root->right, result);
    26     }
    27 };

    3. For every node, check its left subtree nodes whether are all smaller than it and check its right subtree nodes whether are all larger than it.

        Runtime: 20ms.

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     bool isValidBST(TreeNode* root) {
    13         if(!root) return true;
    14         if(!root->left && !root->right) return true;
    15         
    16         if(validLeft(root->left, root->val) && validRight(root->right, root->val)){
    17             return isValidBST(root->left) && isValidBST(root->right);
    18         }
    19         else return false;
    20     }
    21     bool validLeft(TreeNode* root, int max){
    22         if(!root) return true;
    23         if(root->val >= max) return false;
    24         return validLeft(root->left, max) && 
    25                validLeft(root->right, max); //make sure every node in left subtree is smaller than the root value
    26     }
    27     bool validRight(TreeNode* root, int min){
    28         if(!root) return true;
    29         if(root->val <= min) return false;
    30         return validRight(root->left, min) &&
    31                validRight(root->right, min); //make sure every node in the right subtree is larger than the root value
    32     }
    33 };
  • 相关阅读:
    linux的setup命令设置网卡和防火墙等
    在定时任务中慎用pause,否则造成弹窗没关闭,下一次任务不会成功执行
    删除指定文件路径下的所有文件及文件夹
    PHP遍历文件夹及子文件夹所有文件(此外还有飞递归的方法)
    解决wamp、vertrigo等集成环境安装后apache不能启动的问题
    【整理】Virtualbox中的网络类型(NAT,桥接等),网卡,IP地址等方面的设置
    linux IP 设置
    Linux常用命令大全
    在centos命令行下安装软件
    ubuntu12.04安装及配置过程详解1
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4688681.html
Copyright © 2011-2022 走看看