-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0104.cpp
130 lines (118 loc) · 2.32 KB
/
0104.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
/*
int maxDepth(TreeNode* root)
{
int depth = 0;
depth = order(root, depth);
return depth;
}
// 1. 确定参数和返回值
int order(TreeNode* cur, int depth)
{
// 2. 终止条件
if (cur == NULL)
return depth;
else
depth++;
// 3. 确定单层递归的逻辑
int temp1 = order(cur->left, depth);
int temp2 = order(cur->right, depth);
depth = depth < temp1 ? temp1 : depth;
depth = depth < temp2 ? temp2 : depth;
}
int maxDepth(TreeNode* root)
{
return getDepth(root);
}
// 后序遍历 递归
int getDepth(TreeNode* node)
{
if (node == NULL)
return 0;
int leftDepth = getDepth(node->left);
int rightDepth = getDepth(node->right);
// 先求它的左子树的深度,再求右子树的深度
// 最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。
int depth = 1 + max(leftDepth, rightDepth);
return depth;
}
// 先中间结点加1后遍历左右结点
// 先序遍历
int ans;
void getDepth(TreeNode* node, int depth)
{
ans = depth > ans ? depth : ans; // 中
if (node->left == NULL && node->right == NULL)
return;
if (node->left != NULL) // 左
{
depth++; // 深度+1
getDepth(node->left, depth);
depth--; // 回溯 深度-1
}
if (node->right != NULL) // 右
{
depth++;
getDepth(node->right, depth);
depth--;
}
return;
}
int maxDepth(TreeNode* root)
{
ans = 0;
if (root == NULL)
return ans;
getDepth(root, 1);
return ans;
}
*/
// 层序遍历 迭代
int maxDepth(TreeNode* root)
{
int ans = 0;
deque<TreeNode*> que;
if (root != NULL)
que.push_back(root);
while (!que.empty())
{
ans++;
int size = que.size();
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop_front();
if (node->left != NULL)
que.push_back(node->left);
if (node->right != NULL)
que.push_back(node->right);
}
}
return ans;
}
};
int main()
{
TreeNode* root = new TreeNode(1);
TreeNode* node = root;
// node->left = new TreeNode(9);
node->right = new TreeNode(2);
node->right->left = new TreeNode(3);
// node->right->right = new TreeNode(7);
Solution S;
S.maxDepth(root);
return 0;
}