【leetcode】Validate Binary Search Tree(middle)

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.

思路:中序遍历。当前值要比之前的小。

bool isValidBST(TreeNode* root) {
        TreeNode * pPre = NULL;
        TreeNode * pCur = root;
        vector<TreeNode *> v;

        while(!v.empty() || NULL != pCur)
        {
            if(NULL != pCur)
            {
                v.push_back(pCur);
                pCur = pCur->left;
            }
            else
            {
                if(pPre != NULL && v.back()->val <= pPre->val)
                    return false;
                pPre = v.back();
                v.pop_back();
                pCur = pPre->right;
            }
        }
        return true;
    }

大神递归版:注意,每次左子树的值范围在最小值和根值之间,右子树的范围在根植和最大值之间。

public class Solution {
    public boolean isValidBST(TreeNode root) {
        return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }

    public boolean isValidBST(TreeNode root, long minVal, long maxVal) {
        if (root == null) return true;
        if (root.val >= maxVal || root.val <= minVal) return false;
        return isValidBST(root.left, minVal, root.val) && isValidBST(root.right, root.val, maxVal);
    }
}
时间: 2024-11-09 01:39:52

【leetcode】Validate Binary Search Tree(middle)的相关文章

【LeetCode】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

【LeetCode】Validate Binary Search Tree 解题报告

今天CSDN博客发生异常,折腾了大半天终于发出了这篇博文. [题目] 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

【LeetCode】Validate Binary Search Tree 二叉查找树的判断

题目: Given a binary tree, determine if it is a valid binary search tree (BST). 知识点:BST的特点: 1.一个节点的左子树的所有点都小于或等于这个点的值,右子树的所有节点的值大于该节点的值: 2.最左节点值最小,最右节点值最大: 3.中序遍历结果值是一个非降的数列 问题:如果用Integer.MAX_VALUE会过不了测试用例,可是按照TreeNode中的定义,val值也是int呀,没办法用了Long,如果谁知道,麻烦

【LeetCode】二叉查找树 binary search tree(共14题)

链接:https://leetcode.com/tag/binary-search-tree/ p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [220]Contains Duplicate III [315]Count of Smaller Numbers After Self [327]Count of Range Sum [352]Data Stream as Disjoint Intervals [493]

Leetcode 之Validate Binary Search Tree(53)

判断是否是有效的二叉搜索树,即左子树的值小于根结点,右子树的值大于根结点.可以采用递归的方式来完成,递归时如何 传递有效的参数与根结点进行比较,是此题的难点. 1 bool isValidBST(TreeNode *root) 2 { 3 isValidBST(root, INT_MIN, INT_MAX); 4 } 5 bool isValidBST(TreeNode *root, int lower, int upper) 6 { 7 return (root->val > lower &

【LeetCode】 Recover Binary Search Tree BST 中序遍历

题目:Recover Binary Search Tree <span style="font-size:18px;">/* * LeetCode: recover the binary search tree * 题目:二叉树中有两个节点被交换了位置,找出它们,并且将它们换回来,要求用o(n)的连续空间 * 知识点:1.BST树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两

【LeetCode】Recover Binary Search Tree 解题报告

[题目] Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? [解析] 题意:二叉搜索树中,有两个结点的位置

【leetcode】Recover Binary Search Tree

Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 中序

【leetcode】1038. Binary Search Tree to Greater Sum Tree

题目如下: Given the root of a binary search tree with distinct values, modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to node.val. As a reminder, a binary search tree is a t