Leetcode 98. Validate Binary Search Tree

Posted 爱简单的Paul

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 98. 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.

 

Example 1:  

 2
   / \\
  1   3

Binary tree [2,1,3], return true.

 

Example 2:

    1
   / \\
  2   3

Binary tree [1,2,3], return false.

思路一:

利用它本身的性质来做,即左<根<右,初始化时带入系统最大值和最小值,在递归过程中换成它们自己的节点值,用long代替int就是为了包括int的边界条件。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    
     bool isValidBST(TreeNode *root, long mn, long mx) {
        if (!root) return true;
        if (root->val <= mn || root->val >= mx) return false;
        return isValidBST(root->left, mn, root->val) && isValidBST(root->right, root->val, mx);
    }
    
    bool isValidBST(TreeNode* root) {
        return isValidBST(root, LONG_MIN, LONG_MAX);    // 需要判断根节点的值比 左子树的最大值还要大
    }
};

思路二:

这题实际上简化了难度,因为一般的二叉搜索树是左<=根<右,而这道题设定为左<根<右,那么就可以用中序遍历来做。因为如果不去掉左=根这个条件的话,那么下边两个数用中序遍历无法区分:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        if (!root) return true;
        vector<int> vals;
        inorder(root, vals);
        for (int i=0; i<vals.size()-1; ++i) { //判断数组是否满足从大到小的顺序,从而验证该树是否是有效的二叉搜索树
            if (vals[i] >= vals[i+1]) 
                return false;
        }
        return true;
    }
    void inorder(TreeNode *root, vector<int> &vals) { //中序遍历递归
        if (!root) return;
        inorder(root->left, vals);
        vals.push_back(root->val);
        inorder(root->right, vals);
    }
};

 Leetcode 199. Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

For example:
Given the following binary tree,

   1            <---
 /   \\
2     3         <---
 \\     \\
  5     4       <---

 

You should return [1, 3, 4].

这道题要求我们打印出二叉树每一行最右边的一个数字,实际上是求二叉树层序遍历的一种变形,我们只需要保存每一层最右边的数字即可,可以参考我之前的博客 Binary Tree Level Order Traversal 二叉树层序遍历,这道题只要在之前那道题上稍加修改即可得到结果,还是需要用到数据结构队列queue,遍历每层的节点时,把下一层的节点都存入到queue中,每当开始新一层节点的遍历之前,先把新一层最后一个节点值存到结果中,代码如下:

就是要在层序遍历的时候加入每层元素个数的参数来控制求每层的最后一个元素。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        vector<int> res;
        if (root == NULL){
            return res;
        }
        queue<TreeNode*> data;
        data.push(root);
        while(!data.empty()){
            res.push_back(data.back() -> val);          // 每次把栈中的最后一个元素放进去,相当于是每层的最后一个元素值
            int len = data.size();
            for (int i = 0; i < len; i++){
                TreeNode* node1 = data.front();
                data.pop();
                if(node1 -> left) data.push(node1 -> left);
                if(node1 -> right) data.push(node1 -> right);
            }
            
        }
        return res;
    }
};

 

以上是关于Leetcode 98. Validate Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 98. Validate Binary Search Tree

leetcode 98 - Validate Binary Search Tree

Leetcode 98. Validate Binary Search Tree

leetcode [98]Validate Binary Search Tree

LeetCode 98. Validate Binary Search Tree

[LeetCode_98]Validate Binary Search Tree