第十八周:[Leetcode]98. Validate Binary Search Tree
来源:互联网 发布:面试java项目技术难点 编辑:程序博客网 时间:2024/06/06 00:50
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.
Example 1:
2
/ \
1 3
Binary tree [2,1,3], return true.
Example 2:
1
/ \
2 3
Binary tree [1,2,3], return false.
判断是否为有效二叉查找树,用中序遍历二叉树,判断遍历序列是否为升序数组即可。
class Solution {public: vector<int> midorder; bool isValidBST(TreeNode* root) { if(root == NULL) return true; midorder.clear(); dfs(root); for(int i = 0;i < midorder.size() - 1;i++) if(midorder[i] >= midorder[i + 1]) return false; return true; } void dfs(TreeNode* root) { if(root == NULL) return; dfs(root -> left); midorder.push_back(root -> val); dfs(root -> right); }};
阅读全文
0 0
- 第十八周:[Leetcode]98. Validate Binary Search Tree
- LeetCode 98. Validate Binary Search Tree
- [LeetCode]98.Validate Binary Search Tree
- [Leetcode] 98. Validate Binary Search Tree
- LeetCode --- 98. Validate Binary Search Tree
- [leetcode] 98.Validate Binary Search Tree
- [leetcode] 98.Validate Binary Search Tree
- [Leetcode] 98. Validate Binary Search Tree @python
- Leetcode 98. Validate Binary Search Tree
- [LeetCode]98. Validate Binary Search Tree
- leetcode 98. Validate Binary Search Tree
- *LeetCode 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree LeetCode
- LeetCode 98. Validate Binary Search Tree
- LeetCode 98. Validate Binary Search Tree
- LeetCode *** 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree-leetcode-java
- [leetcode] 98. Validate Binary Search Tree
- jmeter个人学习之路--jmeter常用函数整理
- phantomjs实现html生成pdf
- [SQL]无法绑定由多个部分组成的标识符
- B2C商城APP开发方案及流程
- web.xml is missing and <failOnMissingWebXml> is set to true
- 第十八周:[Leetcode]98. Validate Binary Search Tree
- PHP中imagecopyresampled参数详解
- HTML标签语义
- MongoDB数据库设计准则
- Ajax自己的 属性提示错误 error:function(){} $.post().error() .ajaxError()
- bootstap 3.x+springMVC 发生图标不出来,样式还有
- Windows Server 2008 R2安装IIS
- javax.net.ssl.SSLProtocolException: handshake alert: unrecognized_name解决
- 暑期C++03 之匿名对象