LeetCode(98) Validate Binary Search Tree
来源:互联网 发布:js 鼠标点击后div移动 编辑:程序博客网 时间:2024/05/16 05:57
递归算法,确定函数功能及与功能对应的接口。
代码如下:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<int> recursion(TreeNode *root) { if(root->left == NULL && root->right == NULL) { vector<int> result(3, 0); result[0] = 1; result[1] = root->val; result[2] = root->val; return result; } vector<int> result(3, 0); if(root->left != NULL && root->right != NULL) { vector<int> tmpLeft; tmpLeft = recursion(root->left); vector<int> tmpRight; tmpRight = recursion(root->right); if((tmpLeft[0] == 0) || (tmpRight[0] == 0)) { result[0] = 0; return result; }else{ if(tmpLeft[1] >= root->val || tmpRight[2] <= root->val) { result[0] = 0; return result; }else { result[0] = 1; result[1] = tmpRight[1]; result[2] = tmpLeft[2]; return result; } } } if(root->left != NULL && root->right == NULL) { vector<int> tmpLeft; tmpLeft = recursion(root->left); if(tmpLeft[0] == 0) { result[0] = 0; return result; }else { if(tmpLeft[1] >= root->val) { result[0] = 0; return result; }else { result[0] = 1; result[1] = root->val; result[2] = tmpLeft[2]; return result; } } } if(root->left == NULL && root->right != NULL) { vector<int> tmpRight; tmpRight = recursion(root->right); if(tmpRight[0] == 0) { result[0] = 0; return result; }else { if(tmpRight[2] <= root->val) { result[0] = 0; return result; }else { result[0] = 1; result[1] = tmpRight[1]; result[2] = root->val; return result; } } } } bool isValidBST(TreeNode* root) { if(root == NULL) return true; return recursion(root)[0] == 0 ? false : true; }};
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
- Leetcode NO.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(c++)
- 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
- RGB图像转为灰度图
- float to string
- 删除共享
- spring hibernate 整合+项目框架搭建 包含详细注释(清华大学各系统使用代码)
- Xcode离线帮助文档的安装
- LeetCode(98) Validate Binary Search Tree
- html 图片 地图映射
- 设置MyEclipse开发项目时使用的JDK
- java代理模式
- 【UILabel和UIButton】
- MAC 设置环境变量path的几种方法
- android模拟器访问本地ip
- Redis和Memcache对比及选择
- vmstat输出项解释