[leetcode]Validate Binary Search Tree (判断有效二叉搜索树 C语言实现)
来源:互联网 发布:网络攻防大赛平台 编辑:程序博客网 时间:2024/05/20 04:13
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.
OJ’s Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where ‘#’ signifies a path terminator where no node exists below.
Here’s an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as “{1,2,3,#,#,4,#,#,5}”.
题意:判断一棵树是否满足二叉搜索树。
二叉搜索树的特点:左子树<根节点<右子树
解题思路:中序遍历,链栈,来实现。
对于一颗二叉树,中序遍历的结果若满足递增排序就满足二叉搜索树的条件,例如:
一颗二叉树如下:
中序遍历的结果如下:1234567
正好满足二叉搜索树的条件,所以本题采用中序遍历的思路,把遍历的结果存储于链栈中,通过比较前后存入链栈的大小,来判断是否为二叉搜索树。
实现C代码如下:
/** *解题思路:中序遍历,链栈 * Definition for binary tree * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */int flag;struct Node{//创建一个链栈节点 int val; struct Node *next;};struct Stack{//创建一个链栈 struct Node *top;//指向链栈栈顶节点 int count;//记录链栈的节点个数};void InitStack(struct Stack *stack){//初始化一个空栈 stack->count = 0; stack->top = NULL;}void PushStack(struct Stack *stack,int val){//压栈 struct Node *node; node = (struct Node *)malloc(sizeof(struct Node)); if(stack->count > 0){ if(stack->top->val < val){//若不是第一个进栈的节点,则判断与栈顶节点的值大小,若小于栈顶节点值则说明不是二叉搜索树 node->val = val; node->next = stack->top; stack->top = node; stack->count++; }else{ flag = -1;//若不是二叉搜索树设置全局标志位flag为-1; return; } }else{//第一个值进栈 node->val = val; node->next = stack->top; stack->top = node; stack->count++; }}void Inorder(struct TreeNode *root,struct Stack *stack){//中序遍历 if(root == NULL){ return; } Inorder(root->left,stack); PushStack(stack,root->val); Inorder(root->right,stack);}bool isValidBST(struct TreeNode *root) { flag = 0; struct Stack *stack; stack = (struct Stack *)malloc(sizeof(struct Stack)); InitStack(stack); Inorder(root,stack); if(flag == -1){ return 0; } return 1;}
- [leetcode]Validate Binary Search Tree (判断有效二叉搜索树 C语言实现)
- 【Leecode】207Validate Binary Search Tree有效二叉搜索树
- LeetCode | Validate Binary Search Tree(有效的二叉搜索树)
- leetCode 98.Validate Binary Search Tree (有效二叉搜索树) 解题思路和方法
- LeetCode(Validate Binary Search Tree) 判断一个二叉树是否是二叉搜索树
- 判断给定二叉树是否是二叉搜索树(LeetCode: Validate Binary Search Tree)
- 【判断二叉搜索树】Validate Binary Search Tree
- Validate Binary Search Tree 二叉搜索树的合法判断
- LeetCode--Validate Binary Search Tree(二叉搜索树)
- LeetCode 98 Validate Binary Search Tree(判断二叉搜索树)
- 【LeetCode】Validate Binary Search Tree 二叉查找树的判断
- 【LeetCode-面试算法经典-Java实现】【098-Validate Binary Search Tree(验证二叉搜索树)】
- Validate Binary Search Tree 有效的二叉查找树
- 98. Validate Binary Search Tree(判断合法二叉搜索树)
- LeetCode OJ 之 Validate Binary Search Tree (验证二叉搜索树)
- 【LeetCode】98. Validate Binary Search Tree 解法,中序遍历,搜索二叉树合法性
- LeetCode 98. Validate Binary Search Tree(校验二叉搜索树)
- Leetcode 98. Validate Binary Search Tree 验证二叉搜索树 解题报告
- 关于ByteBuffer使用解释
- 递归问题解决汉诺塔(C)
- Web开发人员需知的Web缓存知识
- SQL Server 2008的外围应用配置工具在哪里?
- UILabel 使用
- [leetcode]Validate Binary Search Tree (判断有效二叉搜索树 C语言实现)
- system.Data.SqlClient.SqlError:因为数据库正在使用,所以无法获得对数据库的独占访问权
- win7下,Ant 配合yuicompressor对js和css进行合并、压缩、拷贝处理
- [汇编]_[初级]_[VS2010搭建汇编开发环境win32和x64]
- 黑马程序员——C语言-2:内存剖析
- grunt快速入门
- hdu 3579 Hello Kiki(求解线性同余模方程组的正整数解模板题)
- 屡次失败面试的心理安慰
- NSString 计算显示范围