判断BST

/*
	判断BST
	BST特点:
		小于根节点值在根节点左侧
		大于根节点值在根节点右侧 
*/

#include <iostream>
#include <vector> 

using namespace std;

struct TreeNode
{
	int val;
	struct TreeNode *LeftNode;
	struct TreeNode *RightNode;
	
	TreeNode(int x):
	val(x),LeftNode(NULL),RightNode(NULL){}
};

class Checker
{
	vector<int>vt;
	public:
	/*
		利用中序遍历将节点数据记录下来 
	*/ 
	void search(TreeNode *root)
	{
		if(root!=NULL)
		{
			search(root->LeftNode);
			vt.push_back(root->val);
			search(root->RightNode);
		}	
	} 
	
	bool checkBST(TreeNode *root)
	{
		for(int i=0;i<vt.size()-1;i++)
		{
			if(vt[i]>vt[i+1])
			{
				return false;
			}
		}	
		return true;			
	}
}

int main(int argc, char *argv[])
{
	return 0;
}

  

原文地址:https://www.cnblogs.com/achao123456/p/7568865.html