50
50
51
51
代码如下:
52
52
``` CPP
53
- int getdepth (treenode * node)
53
+ int getdepth (TreeNode * node)
54
54
```
55
55
56
56
2. 确定终止条件:如果为空节点的话,就返回0,表示高度为0。
@@ -76,14 +76,14 @@ return depth;
76
76
``` CPP
77
77
class solution {
78
78
public:
79
- int getdepth(treenode * node) {
79
+ int getdepth(TreeNode * node) {
80
80
if (node == NULL) return 0;
81
81
int leftdepth = getdepth(node->left); // 左
82
82
int rightdepth = getdepth(node->right); // 右
83
83
int depth = 1 + max(leftdepth, rightdepth); // 中
84
84
return depth;
85
85
}
86
- int maxdepth(treenode * root) {
86
+ int maxDepth(TreeNode * root) {
87
87
return getdepth(root);
88
88
}
89
89
};
@@ -93,9 +93,9 @@ public:
93
93
```CPP
94
94
class solution {
95
95
public:
96
- int maxdepth(treenode * root) {
96
+ int maxDepth(TreeNode * root) {
97
97
if (root == null) return 0;
98
- return 1 + max(maxdepth (root->left), maxdepth (root->right));
98
+ return 1 + max(maxDepth (root->left), maxDepth (root->right));
99
99
}
100
100
};
101
101
@@ -110,7 +110,7 @@ public:
110
110
class solution {
111
111
public:
112
112
int result;
113
- void getdepth(treenode * node, int depth) {
113
+ void getdepth(TreeNode * node, int depth) {
114
114
result = depth > result ? depth : result; // 中
115
115
116
116
if (node->left == NULL && node->right == NULL) return ;
@@ -127,7 +127,7 @@ public:
127
127
}
128
128
return ;
129
129
}
130
- int maxdepth(treenode * root) {
130
+ int maxDepth(TreeNode * root) {
131
131
result = 0;
132
132
if (root == NULL) return result;
133
133
getdepth(root, 1);
@@ -144,7 +144,7 @@ public:
144
144
class solution {
145
145
public:
146
146
int result;
147
- void getdepth(treenode * node, int depth) {
147
+ void getdepth(TreeNode * node, int depth) {
148
148
result = depth > result ? depth : result; // 中
149
149
if (node->left == NULL && node->right == NULL) return ;
150
150
if (node->left) { // 左
@@ -155,7 +155,7 @@ public:
155
155
}
156
156
return ;
157
157
}
158
- int maxdepth(treenode * root) {
158
+ int maxDepth(TreeNode * root) {
159
159
result = 0;
160
160
if (root == 0) return result;
161
161
getdepth(root, 1);
@@ -182,16 +182,16 @@ c++代码如下:
182
182
```CPP
183
183
class solution {
184
184
public:
185
- int maxdepth(treenode * root) {
185
+ int maxDepth(TreeNode * root) {
186
186
if (root == NULL) return 0;
187
187
int depth = 0;
188
- queue<treenode *> que;
188
+ queue<TreeNode *> que;
189
189
que.push(root);
190
190
while(!que.empty()) {
191
191
int size = que.size();
192
192
depth++; // 记录深度
193
193
for (int i = 0; i < size; i++) {
194
- treenode * node = que.front();
194
+ TreeNode * node = que.front();
195
195
que.pop();
196
196
if (node->left) que.push(node->left);
197
197
if (node->right) que.push(node->right);
@@ -230,11 +230,11 @@ c++代码:
230
230
```CPP
231
231
class solution {
232
232
public:
233
- int maxdepth(node * root) {
233
+ int maxDepth(Node * root) {
234
234
if (root == 0) return 0;
235
235
int depth = 0;
236
236
for (int i = 0; i < root->children.size(); i++) {
237
- depth = max (depth, maxdepth (root->children[ i] ));
237
+ depth = max (depth, maxDepth (root->children[ i] ));
238
238
}
239
239
return depth + 1;
240
240
}
@@ -247,15 +247,15 @@ public:
247
247
```CPP
248
248
class solution {
249
249
public:
250
- int maxdepth(node * root) {
251
- queue<node *> que;
250
+ int maxDepth(Node * root) {
251
+ queue<Node *> que;
252
252
if (root != NULL) que.push(root);
253
253
int depth = 0;
254
254
while (!que.empty()) {
255
255
int size = que.size();
256
256
depth++; // 记录深度
257
257
for (int i = 0; i < size; i++) {
258
- node * node = que.front();
258
+ Node * node = que.front();
259
259
que.pop();
260
260
for (int j = 0; j < node->children.size(); j++) {
261
261
if (node->children[j]) que.push(node->children[j]);
0 commit comments