根据树的后序判断是不是二叉搜索树&&二叉树中和为某一值的路径
来源:互联网 发布:国家电网 辞职 知乎 编辑:程序博客网 时间:2024/05/17 06:31
根据树的后序判断是不是二叉搜索树。
关键树的后序序列规律是根是最后的,而右子树都大于根,左子树都小于根。可以判断序列是否满足这个条件,然后递归判断左右子树即可。
#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");elseprintf("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");elseprintf("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
- 根据树的后序判断是不是二叉搜索树&&二叉树中和为某一值的路径
- 剑指Offer面试题24二叉搜索树的后序遍历序列(递归),面试题25二叉树中和为某一值的路径(栈)
- 【树】二叉树中和为某一值的路径
- 【树6】二叉树中和为某一值的路径
- 题目11:二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的所有路径
- 题目1368:二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- 1368:二叉树中和为某一值的路径 @jobdu
- 二叉树中和为某一值的路径
- 二叉树中和为某一值的路径
- Q25:二叉树中和为某一值的路径
- Android 【信号格的客制化】Part 2:客制化修改代码说明与示例
- android动画
- hdu4756 最小树+树形dp
- crontab -e 设备空间不足解决方法
- 转载:linux 块驱动学习(三)
- 根据树的后序判断是不是二叉搜索树&&二叉树中和为某一值的路径
- RPG互动小说的灵感来源
- 小编码大阻力
- vim +sed
- 理财技术
- android selector 背景选择器的使用
- Android 【信号格的客制化】Part 3:手机上显示的信号强度dbm值不正确
- 初用Java
- JAVA5多线程---Condition使用---线程通信