leetcode98

class Solution {
public:
vector<int> V;
void postTree(TreeNode* node) {
if (node != NULL) {
if (node->left != NULL) {
postTree(node->left);
}
V.push_back(node->val);
if (node->right != NULL) {
postTree(node->right);
}
}
}
bool isValidBST(TreeNode* root) {
postTree(root);
for (int i = ; i < V.size(); i++) {
if (V[i] <= V[i - ]) {
return false;
}
}
return true;
}
};

补充一个python的实现:

 class Solution:
def __init__(self):
self.lists = [] def inOrder(self,root):
if root != None:
if root.left != None:
self.inOrder(root.left)
self.lists.append(root.val)
if root.right != None:
self.inOrder(root.right) def isValidBST(self, root: 'TreeNode') -> 'bool':
self.inOrder(root)
n = len(self.lists)
for i in range(n-):
if self.lists[i] >= self.lists[i+]:
return False
return True
上一篇:SHELL学习笔记三


下一篇:Python基础之文件处理、函数、内置函数 (三)