LeetCode 98 Validate Binary Search Tree判断是否为合法二叉树

 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 Left(TreeNode* left, int root){
13         while(left->right != NULL){
14             left = left->right;
15         }
16         return left->val < root;
17     }
18     bool Right(TreeNode* right,int root){
19         while(right->left != NULL)
20             right = right->left;
21         return right->val > root;
22     }
23
24     bool isValidBST(TreeNode* root) {
25         if(root == NULL) return true;
26         int left = 0, right = 0;
27         bool lres = true,rres = true;
28         if(root->left){//不为空的情况下
29             left = 1;
30             if(root->left->val >= root->val) return false;
31             lres = isValidBST(root->left);
32             if(lres) lres = Left(root->left,root->val);//在左儿子是合法二叉树的情况下,检验左儿子的最大值(即一直取左儿子的右儿子)是否小于root值
33             else return false;
34         }
35         if(root->right){
36             right = 1;
37             if(root->right->val <= root->val) return false;
38             rres = isValidBST(root->right);
39             if(rres) rres = Right(root->right,root->val);
40             else return false;
41         }
42         return lres && rres;
43     }
44
45 };

合法二叉树条件:1、左儿子均小于根,右儿子均大于根 2、左儿子、右儿子均为合法二叉树

时间: 2024-10-18 16:47:53

LeetCode 98 Validate Binary Search Tree判断是否为合法二叉树的相关文章

[Lintcode]95. Validate Binary Search Tree/[Leetcode]98. Validate Binary Search Tree

95. Validate Binary Search Tree/98. Validate Binary Search Tree 本题难度: Easy Topic: Binary Tree Description 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 contain

leetcode 98 Validate Binary Search Tree ----- java

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]98. Validate Binary Search Tree验证BST

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】98. Validate Binary Search Tree -判断是否为二叉排序树

一.描述: 二.思路: 二叉排序树(BST),中序遍历的结果一定是非递减序列(来自百度百科): 本题中对于BST的定义是要么大于,要么小与,即遍历结果只能是递增序列,故可以通过判断中序遍历的结果序列是否是递增序列,来判断是否为合法BST: 另一种方法是使用递归: 三.代码: 1.非递归,通过中序遍历结果判断: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left;

[Leetcode][BST][Validate Binary Search Tree]

判断一颗树是不是二分查找树,非常经典基础的一个算法. 我很久之前第一次做的时候,是先求出来了树的前序遍历的结果,然后判断这个数组排序后是否和排序前相同,还要判断重复虾米的,很纠结的一种做法. 后来思考了一下怎么用递归的思路做,觉得应该根据定义返回两个子树的最大值和最小值,写了一会代码,发现好麻烦,不太对的样子. 后来看了题解,发现是用了一种反向的思维,把上下界从树的顶端传下去,而不是自下而上的约束.作者太机智了. 1 /** 2 * Definition for binary tree 3 *

[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

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 OJ - 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

leetcode dfs Validate Binary Search Tree

Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My 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