1 #include
2 #include
3 struct TreeNode
4 {
5 int val;
6 struct TreeNode *left;
7 struct TreeNode *right;
8 };
9 struct TreeNode * ClimbAsWidth(struct TreeNode *root, int n)
10 {
11 struct TreeNode * temp;
12 if(root->val==n)
13 return root;
14 if(root->left==NULL&&root->right==NULL)
15 return NULL;
16 if(root->left!=NULL)
17 return ClimbAsWidth(root->left, n);
18 if(root->right!=NULL)
19 return ClimbAsWidth(root->right, n);
20 if((temp=ClimbAsWidth(root->left, n))!=NULL)
21 return temp;
22 else if((temp=ClimbAsWidth(root->right, n))!=NULL)
23 return temp;
24 else
25 return NULL;
26 }
27
28 struct TreeNode* searchBST(struct TreeNode* root, int val)
29 {
30 return ClimbAsWidth(root, val);
31 }
32
33 int main()
34 {
35 struct TreeNode root;
36 root.val = 4;
37 root.left = malloc(sizeof(struct TreeNode));
38 root.left->val = 2;
39 root.right = malloc(sizeof(struct TreeNode));
40 root.right->val = 7;
41 root.right->left = NULL;
42 root.right->right = NULL;
43 root.left->left = malloc(sizeof(struct TreeNode));
44 root.left->left->val = 1;
45 root.left->left->left = NULL;
46 root.left->left->right = NULL;
47 root.left->right = malloc(sizeof(struct TreeNode));
48 root.left->left->val = 3;
49 root.left->left->left = NULL;
50 root.left->left->right = NULL;
51 if(searchBST(&root,7)==/*root.right*/NULL)
52 puts("rigth");
53 return 0;
54 }