根据树的后序判断是不是二叉搜索树&&二叉树中和为某一值的路径

根据树的后序判断是不是二叉搜索树。

关键树的后序序列规律是根是最后的,而右子树都大于根,左子树都小于根。可以判断序列是否满足这个条件,然后递归判断左右子树即可。

#include<iostream>
#include<string>
using namespace std;
bool IsSearchBinaryTree(int *numbers,int begin,int end){
	if(numbers == NULL || begin>end)
		return false;
	if(begin == end)
		return true;
	int root = numbers[end];
	int i = end-1;
	bool bleft,bright;
	//右子树都大于根值
	while(i>=begin && numbers[i] > root)
		i--;
	int j = i;
	//左子树都小于根值
	while(j>=begin && numbers[j] <root)
		j--;
	//如果还有结点剩余则不满足搜索二叉树性质
	if(j >=begin)
		return false;
	//判断左右子树是否为搜索二叉树
	bleft = bright = true;
	if(i >= begin)
		bleft = IsSearchBinaryTree(numbers,begin,i);
	if(i <end-1)
		bright= IsSearchBinaryTree(numbers,i+1,end-1);
	return (bleft && bright);
}
int main(){
	bool test1,test2;
	int number1[] = {5,7,6,9,11,10,8};
	test1 = IsSearchBinaryTree(number1,0,6);
	if(test1)
		printf("number1 is binary search sequence\n");
	else
		printf("number1 is not binary search sequence\n");
	int number2[] = {2,3,5,4,1,2,8};
	test2 = IsSearchBinaryTree(number2,0,6);
	if(test2)
		printf("number2 is binary search sequence\n");
	else
		printf("number2 is not binaru search sequence\n");
	system("PAUSE");
	return 0;
}

路径指的是从根到叶子结点。可以用dfs。

#include<iostream>
#include<string>
#include<queue>
using namespace std;
struct Binarytree{  
    int value;  
    Binarytree *left;  
    Binarytree *right;  
};  
int path[20];
Binarytree* Buildtree(){  
    int x;  
    scanf("%d",&x);  
    if(x == 0)  
        return NULL;  
    queue<Binarytree *> Bq;  
    Binarytree* root = (Binarytree *)malloc(sizeof(Binarytree));  
    root->value = x;  
    root->left = NULL;  
    root->right = NULL;  
    Binarytree* temp = root;  
    Bq.push(temp);  
    while(!Bq.empty()){  
        temp = Bq.front();  
        Bq.pop();  
        if(temp->left == NULL){  
            int x;  
            printf("输入%d的左结点\n",temp->value);  
            scanf("%d",&x);  
            if(x!=0){  
                Binarytree * tleft = (Binarytree *)malloc(sizeof(Binarytree));  
                tleft->value = x;  
                tleft->left = tleft->right = NULL;  
                temp->left = tleft;  
                Bq.push(tleft);  
            }  
        }  
        if(temp->right == NULL){  
            int x;  
            printf("输入%d的右结点\n",temp->value);  
            scanf("%d",&x);  
            if(x!=0){  
                Binarytree * tright = (Binarytree *)malloc(sizeof(Binarytree));  
                tright->value = x;  
                tright->left = tright->right = NULL;  
                temp->right = tright;  
                Bq.push(tright);  
            }  
        }  
    }  
    return root;  
}  
//sum表示要求的路径值,current和path用来记录路径
void printtree(int sum,int current,int path[],Binarytree* root){
	if(sum <0)
		return;
	path[current] = root->value;
	++current;
	sum -= root->value;
	if(sum == 0 && root->left==NULL && root->right == NULL){
		printf("find a path:\n");
		for(int i =0;i<current;i++)
			printf("%d ",path[i]);
		printf("\n");
		return;
	}
	if(root->left)
		printtree(sum,current,path,root->left);
	if(root->right)
		printtree(sum,current,path,root->right);
}
int main(){
	Binarytree *root = Buildtree();
	printtree(22,0,path,root);
	system("PAUSE");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值