leetcode 刷题之路 29 Validate Binary Search Tree
来源:互联网 发布:拳皇13出招优化补丁 编辑:程序博客网 时间:2024/05/01 16:58
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.
题目要求判断一个二叉树是否为二叉查找树。
如果一个二叉树进行中序遍历后得到的结果为一个升序序列,则该二叉树为二叉查找树。
测试通过代码:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:bool isValidBST(TreeNode *root) {TreeNode *lastNode = NULL;return helper(root, lastNode);}static bool helper(TreeNode *root, TreeNode *&lastNode){if (root == NULL)return true;bool b1 = helper(root->left, lastNode);bool b2 = true;if (lastNode!=NULL)b2 = lastNode->val < root->val;lastNode = root;bool b3 = helper(root->right, lastNode);return b1&&b2&&b3;}};
0 0
- leetcode 刷题之路 29 Validate Binary Search Tree
- leetcode之Validate Binary Search Tree
- leetcode 之 Validate Binary Search Tree
- LeetCode之Validate Binary Search Tree
- leetcode之Validate Binary Search Tree
- 【Leetcode】之Validate Binary Search Tree
- [leetcode刷题系列]Validate Binary Search Tree
- LeetCode刷题笔录 Validate Binary Search Tree
- 开始刷题leetcode day37:Validate Binary Search Tree
- LeetCode: Validate Binary Search Tree
- [Leetcode] Validate Binary Search Tree
- LeetCode : Validate Binary Search Tree
- [LeetCode] Validate Binary Search Tree
- [LeetCode] Validate binary search tree
- [LeetCode]Validate Binary Search Tree
- [Leetcode]Validate Binary Search Tree
- [leetcode]Validate Binary Search Tree
- LeetCode-Validate Binary Search Tree
- vector hash arraylist list
- asp.net连接MYSQL的SQL语句中有汉字问题最简单、最终的解决方法
- Objective-c 代理模式
- 谈一谈自己对依赖、关联、聚合和组合之间区别的理解
- 暑假第十七天,7月23日
- leetcode 刷题之路 29 Validate Binary Search Tree
- 本屌的博客就这样开通了~·~
- swift-集合测试题答案
- ERROR 1044 (42000) 错误解决
- win8 64bit eclipse3.5.1 jdk1.7 等都是64bit,Java Decompiler插件安装
- HDU 1062 Text Reverse
- Ubuntu下载Android系统源码
- new, operator new, placement new
- 剖析SalesForce的多租户架构(PAAS\SAAS\云计算)