5.3.3—二叉查找树—Validate Binary Sear Tree
来源:互联网 发布:mmd双人动作数据 编辑:程序博客网 时间:2024/06/08 09:19
描述
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
#include "BinaryTree.h"#include<algorithm>#include<vector>#include<stack>using namespace std;vector<int> path;//===判断二叉树是否为BSTbool IsBST(BinaryTreeNode *proot){if ((proot->m_pLeft==NULL&&proot->m_pRight==NULL)||proot==NULL)return true;if (proot->m_pLeft != NULL&&proot->m_pRight != NULL){return (proot->m_pLeft->m_nValue < proot->m_nValue&&proot->m_nValue < proot->m_pRight->m_nValue)&& IsBST(proot->m_pLeft) && IsBST(proot->m_pRight);}else if (proot->m_pLeft != NULL&&proot->m_pRight == NULL){return (proot->m_pLeft->m_nValue < proot->m_nValue)&& IsBST(proot->m_pLeft);}else if (proot->m_pLeft == NULL&&proot->m_pRight != NULL){return (proot->m_nValue < proot->m_pRight->m_nValue)&& IsBST(proot->m_pRight);}}// ====================测试代码====================// 9// 6 10// 3 8 // 1 5// 0int main(){//===BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);BinaryTreeNode* pNode0 = CreateBinaryTreeNode(0);ConnectTreeNodes(pNode9, pNode6, pNode10);ConnectTreeNodes(pNode6, pNode3, pNode8);ConnectTreeNodes(pNode3, pNode1, pNode5);ConnectTreeNodes(pNode1, pNode0, NULL);//===//PrintTree(pNode8);//===bool flag = IsBST(pNode9);cout << flag << endl;DestroyTree(pNode8);}
阅读全文
0 0
- 5.3.3—二叉查找树—Validate Binary Sear Tree
- 5.3.4—二叉查找树—Convert Sorted Array to Binary Sear Tree
- 5.3.5—二叉查找树—Convert Sorted List to Binary Sear Tree
- 5.1.7—二叉树的遍历—Recover Binary Sear Tree
- 二叉查找树——Validate Binary Search Tree
- 判断二叉查找树的合法性 Validate Binary Search Tree
- Validate Binary Search Tree 有效的二叉查找树
- 【LeetCode】Validate Binary Search Tree 二叉查找树的判断
- Leetcode 98 Validate Binary Search Tree 验证二叉查找树
- lintcode validate-binary-search-tree 验证二叉查找树
- **[Lintcode]Validate Binary Search Tree 验证二叉查找树
- 【LeetCode】Validate Binary Search Tree 验证二叉查找树
- 5.3.5 Convert Sorted List to Binary Sear Tree
- 5.1.5 Recover Binary Sear Tree
- Validate Binary Search Tree--判断一个树是不是二叉查找树(重重重)
- 95.Validate Binary Search Tree-验证二叉查找树(中等题)
- 7.1—查找—Sear for a Range
- 7.2—查找—Sear Insert Position
- Codeforces837C two seals
- 快速查找素数 nyoj 187
- 在我眼中vue/vux的钩子
- IOS与其他操作系统中静态库与动态库的用法总结
- 5.2.2—二叉树的构建—Construct Binary Tree from Inorder and Postorder Traversal
- 5.3.3—二叉查找树—Validate Binary Sear Tree
- [VC.NET]托管代码和非托管代码
- 5.3.4—二叉查找树—Convert Sorted Array to Binary Sear Tree
- 5.3.5—二叉查找树—Convert Sorted List to Binary Sear Tree
- 动态规划中级教程 343. Integer Break
- 大型复杂信息系统研发的大学式组织管理方法
- UVA11754 Code Feat
- 坦克大战学习笔记
- 5.4.1—二叉树的递归—Minimum Depth of Binary Tree