LeetCode_Validate Binary Search Tree

一.題目

Validate Binary Search Tree

  Total Accepted: 51751 Total Submissions: 251219My Submissions

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.

Show Tags
Have you met this question in a real interview?  
Yes
 
No

Discuss











二.解題技巧

    這道題是判斷一個二叉查找樹是否合法的,這個可以根據二叉查找樹的定義來進行判斷,即一個內部結點的值要大於左子樹的最大值,同時要小於右子樹的最大值,根據這個定義,就可以遞歸得進行判斷了,這種方法得時間複雜度爲O(n),空間複雜度爲O(logn)。這道題要注意的地方就是要記得更新以結點爲父節點的樹的最大值和最小值,以便遞歸返回時給上一個調用進行判斷。


三.實現代碼

#include <iostream>

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/


struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};


class Solution
{
private:
    bool isValidBST(TreeNode* root, int &MinValue, int &MaxValue)
    {
        if (!root)
        {
            return true;
        }


        if (root->left)
        {
            if (root->val <= root->left->val)
            {
                return false;
            }

            int LeftMinValue = 0;
            int LeftMaxValue = 0;
            if (!isValidBST(root->left, LeftMinValue, LeftMaxValue))
            {
                return false;
            }
            else
            {
                MinValue = LeftMinValue;
                if (LeftMaxValue != LeftMinValue)
                {
                    if (root->val <= LeftMaxValue)
                    {
                        return false;
                    }
                }
            }
        }
        else
        {
            MinValue = root->val;
        }


        if (root->right)
        {
            if (root->val >= root->right->val)
            {
                return false;
            }

            int RightMinValue = 0;
            int RightMaxValue = 0;

            if (!isValidBST(root->right, RightMinValue, RightMaxValue))
            {
                return false;
            }
            else
            {
                MaxValue = RightMaxValue;
                if (RightMaxValue != RightMinValue)
                {
                    if (root->val >= RightMinValue)
                    {
                        return false;
                    }
                }
            }
        }
        else
        {
            MaxValue = root->val;
        }

        return true;
    }

public:
    bool isValidBST(TreeNode* root)
    {
        int MinValue = 0;
        int MaxValue = 0;
        bool IsLeaf = true;

        return isValidBST(root, MinValue, MaxValue);
    }
};






四.體會

     這道題主要還是二叉查找樹的遍歷,同時,要將子樹的最大值和最小值返回,以便判讀結點的值是否合法,同時要記得更新以該結點爲父結點的子樹的最大值和最小值,以便遞歸返回時給上一個調用進行判斷。這道題並不難,只要注意細節即可。



版權所有,歡迎轉載,轉載請註明出處,謝謝微笑





發佈了190 篇原創文章 · 獲贊 48 · 訪問量 44萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章