- Notifications
You must be signed in to change notification settings - Fork 117
/
Copy path104.c
48 lines (42 loc) · 1 KB
/
104.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<stdio.h>
#include<stdlib.h>
structTreeNode {
intval;
structTreeNode*left;
structTreeNode*right;
};
intmaxDepth(structTreeNode*root) {
if (root==NULL)
return0;
intl=maxDepth(root->left);
intr=maxDepth(root->right);
if (l>r)
returnl+1;
else
returnr+1;
}
voidprintTreePreOrder(structTreeNode*p) {
if (p!=NULL) {
printf("%d", p->val);
printTreePreOrder(p->left);
printTreePreOrder(p->right);
}
elseprintf("#");
}
intmain() {
structTreeNode*t= (structTreeNode*)calloc(4, sizeof(structTreeNode));
structTreeNode*p=t;
p->val=1;
p->left=++t;
t->val=2;
t->left=t->right=NULL;
p->right=++t;
t->val=3;
p->right->left=NULL;
p->right->right=++t;
t->val=4;
t->left=t->right=NULL;
printTreePreOrder(p); printf("\n");
printf("%d\n", maxDepth(p));
return0;
}