-
Notifications
You must be signed in to change notification settings - Fork 0
/
104_二叉树的最大深度.c
93 lines (84 loc) · 1.52 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* @lc app=leetcode.cn id=104 lang=c
*
* [104] 二叉树的最大深度
*
* https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/
*
* algorithms
* Easy (77.85%)
* Likes: 1863
* Dislikes: 0
* Total Accepted: 1.4M
* Total Submissions: 1.8M
* Testcase Example: '[3,9,20,null,null,15,7]'
*
* 给定一个二叉树 root ,返回其最大深度。
*
* 二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
*
*
*
* 示例 1:
*
*
*
*
*
*
* 输入:root = [3,9,20,null,null,15,7]
* 输出:3
*
*
* 示例 2:
*
*
* 输入:root = [1,null,2]
* 输出:2
*
*
*
*
* 提示:
*
*
* 树中节点的数量在 [0, 10^4] 区间内。
* -100 <= Node.val <= 100
*
*
*/
// @lc code=start
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
int maxDepth(struct TreeNode* root) {
int dleft = 0, dright = 0;
if(!root)
return 0;
if(root->left != NULL)
dleft = maxDepth(root->left) + 1;
else
dleft = 1;
if(root->right != NULL)
dright = maxDepth(root->right) + 1;
else
dright = 1;
return dleft > dright ? dleft : dright;
}
// @lc code=end