6-20 No Less Than X in BST(20 point(s))
You are supposed to output, in decreasing order, all the elements no less thanX
in a binary search tree T
.
Format of function:
void Print_NLT( Tree T, int X );
where Tree
is defined as the following:
typedef struct TreeNode *Tree;
struct TreeNode {
int Element;
Tree Left;
Tree Right;
};
The function is supposed to use Output(X)
to print X
.
Sample program of judge:
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode *Tree;
struct TreeNode {
int Element;
Tree Left;
Tree Right;
};
Tree BuildTree(); /* details omitted */
void Output( int X ); /* details omitted */
void Print_NLT( Tree T, int X );
int main()
{
Tree T;
int X;
T = BuildTree();
scanf("%d", &X);
Print_NLT( T, X );
printf("End\n");
return 0;
}
/* Your function will be put here */
Sample Output 1 (for the tree shown in Figure 1):
92 91 90 85 81 80 End
Figure 1
Sample Output 2 (for the tree shown in Figure 2):
End
Figure 2
code:
void Print_NLT( Tree T, int X ){
if(T == NULL)return;//如果树为空,返回null
//直接进行树的遍历,因为是从大到小输出不小于X的数
//所以先遍历右子树,大于X的直接输出,再遍历左子树.
Print_NLT(T->Right,X);
if(T->Element>=X){
printf("%d ",T->Element);
}
Print_NLT(T->Left,X);
}