【题解】【BST】【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 greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
思路:
这题其实考查的是BST的定义,下面code的是CC150v5上的第一种答案简化版,同样借鉴中序遍历的思想,但是只保存当前点上家的值,判断当前点是否大于上家。
代码:
bool isValidBST(TreeNode *root) {
int init = INT_MIN;
return isValidNode(root, init);
}
bool isValidNode(TreeNode *root, int& lastVal){
if(root == NULL)
return true; if(!isValidNode(root->left, lastVal))
return false; if(root->val <= lastVal) //不是<而是<=,failed {1,1}=>false
return false;
lastVal = root->val; if(!isValidNode(root->right, lastVal))
return false; return true;//忘写最后一句了
}
【题解】【BST】【Leetcode】Validate Binary Search Tree的更多相关文章
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- [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 ...
- [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 ...
- 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 ...
- LeetCode: Validate Binary Search Tree [098]
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- LeetCode :: Validate Binary Search Tree[具体分析]
Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less th ...
- [leetcode]Validate Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/ 题意:检测一颗二叉树是否是二叉查找树. 解题思路:看到二叉树我们首 ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- javascript 判断身份证的正确性
function isIdCardNo(vals) { var cardNum = vals; if (cardNum.length == 0) { return true; } // 11-15,2 ...
- 使用BroadcastReceiver实现系统对手机电量进行提示
import android.content.BroadcastReceiver;import android.content.Context;import android.content.Inten ...
- eclipse快捷键失效的解决办法
今天敲html代码,突然发现ctrl+D不能用了,shift+enter/shift+ctrl+enter也不能用了,上网上搜了下,原来我是在文本模式下打开的.切换为html editor打开就o了. ...
- thinking in java之Collections工具类的使用
代码摘自<thinking in java>4td 此实例非常好的总结了Collections的一些常见方法的使用. package countainers; import java.ut ...
- Android的SwipeToDismiss第三方开源框架模拟QQ对话列表侧滑删除,置顶,将头像图片圆形化处理。
<Android SwipeToDismiss:左右滑动删除ListView条目Item> Android的SwipeToDismiss是github上一个第三方开源框架(github ...
- (转载)全球唯一标识GUID
GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值. GUID ...
- WDCP管理面板安装启动EXIF、bcmath完整步骤
一般我们网站建设的需要,如果使用WDCP面板默认的功能就足够使用,如果需要特殊程序的特定组件支持,就需要独立的安装支持组件.比如一位朋友的程序需要支持EXIF.bcmath组件,这不老蒋寻找解决方法, ...
- Opencv中在图片上显示文本
1.cvPutText函数(在图像中加入文本) void cvPutText( CvArr* img, const char* text, CvPoint org, const CvFont* fon ...
- [pjsip]Pjlib中配置文件config.h解析
config_site.h 这个头文件包含在config.h中,用于引入平台?(site)/用户特定的配置以控制PJLIB的特性,用户需要自己生成这个文件. 譬如说我们要把PJLIB编译成DLL,那么 ...
- ssm开发的一点小技巧
一般使用反转工作生成基础bean如Items然后我们使用的实体类一般是基础bean的拓展类ItemsCustomer,继承自基础类,这个是为了方便对于表字段的更改生成的bean影响减低我们查询一般是使 ...