-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path104.maximum-depth-of-binary-tree-bottom-up.c
53 lines (44 loc) · 1.43 KB
/
104.maximum-depth-of-binary-tree-bottom-up.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
49
50
51
52
53
#include <stdlib.h>
#include <stdio.h>
/**
* Definition for a binary tree node.
*/
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int maxDepth(struct TreeNode *root)
{
if (root == NULL)
return 0;
int depth_left = maxDepth(root->left);
int depth_right = maxDepth(root->right);
return MAX(depth_left, depth_right) + 1;
}
int main(int argc, char const *argv[])
{
struct TreeNode *rootRightLeftRight = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootRightLeftRight->val = 4;
rootRightLeftRight->left = rootRightLeftRight->right = NULL;
struct TreeNode *rootRightLeftLeft = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootRightLeftLeft->val = 5;
rootRightLeftLeft->left = rootRightLeftLeft->right = NULL;
struct TreeNode *rootRightLeft = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootRightLeft->val = 3;
rootRightLeft->left = rootRightLeftLeft;
rootRightLeft->right = rootRightLeftRight;
struct TreeNode *rootRight = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootRight->val = 2;
rootRight->left = rootRightLeft;
rootRight->right = NULL;
struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->val = 1;
root->left = NULL;
root->right = rootRight;
int depth = maxDepth(root);
printf("%d\n", depth);
return 0;
}