Update 0104.二叉树的最大深度.md

c++语言部分,函数名和节点命名大小写有错误
This commit is contained in:
ayao98
2023-03-31 15:50:58 +08:00
committed by GitHub
parent f0db0605f3
commit f7988e06dc

View File

@ -50,7 +50,7 @@
代码如下:
```CPP
int getdepth(treenode* node)
int getdepth(TreeNode* node)
```
2. 确定终止条件如果为空节点的话就返回0表示高度为0。
@ -76,14 +76,14 @@ return depth;
```CPP
class solution {
public:
int getdepth(treenode* node) {
int getdepth(TreeNode* node) {
if (node == NULL) return 0;
int leftdepth = getdepth(node->left); // 左
int rightdepth = getdepth(node->right); // 右
int depth = 1 + max(leftdepth, rightdepth); // 中
return depth;
}
int maxdepth(treenode* root) {
int maxDepth(TreeNode* root) {
return getdepth(root);
}
};
@ -93,9 +93,9 @@ public:
```CPP
class solution {
public:
int maxdepth(treenode* root) {
int maxDepth(TreeNode* root) {
if (root == null) return 0;
return 1 + max(maxdepth(root->left), maxdepth(root->right));
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};
@ -110,7 +110,7 @@ public:
class solution {
public:
int result;
void getdepth(treenode* node, int depth) {
void getdepth(TreeNode* node, int depth) {
result = depth > result ? depth : result; // 中
if (node->left == NULL && node->right == NULL) return ;
@ -127,7 +127,7 @@ public:
}
return ;
}
int maxdepth(treenode* root) {
int maxDepth(TreeNode* root) {
result = 0;
if (root == NULL) return result;
getdepth(root, 1);
@ -144,7 +144,7 @@ public:
class solution {
public:
int result;
void getdepth(treenode* node, int depth) {
void getdepth(TreeNode* node, int depth) {
result = depth > result ? depth : result; // 中
if (node->left == NULL && node->right == NULL) return ;
if (node->left) { // 左
@ -155,7 +155,7 @@ public:
}
return ;
}
int maxdepth(treenode* root) {
int maxDepth(TreeNode* root) {
result = 0;
if (root == 0) return result;
getdepth(root, 1);
@ -182,16 +182,16 @@ c++代码如下:
```CPP
class solution {
public:
int maxdepth(treenode* root) {
int maxDepth(TreeNode* root) {
if (root == NULL) return 0;
int depth = 0;
queue<treenode*> que;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()) {
int size = que.size();
depth++; // 记录深度
for (int i = 0; i < size; i++) {
treenode* node = que.front();
TreeNode* node = que.front();
que.pop();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
@ -230,11 +230,11 @@ c++代码:
```CPP
class solution {
public:
int maxdepth(node* root) {
int maxDepth(Node* root) {
if (root == 0) return 0;
int depth = 0;
for (int i = 0; i < root->children.size(); i++) {
depth = max (depth, maxdepth(root->children[i]));
depth = max (depth, maxDepth(root->children[i]));
}
return depth + 1;
}
@ -247,15 +247,15 @@ public:
```CPP
class solution {
public:
int maxdepth(node* root) {
queue<node*> que;
int maxDepth(Node* root) {
queue<Node*> que;
if (root != NULL) que.push(root);
int depth = 0;
while (!que.empty()) {
int size = que.size();
depth++; // 记录深度
for (int i = 0; i < size; i++) {
node* node = que.front();
Node* node = que.front();
que.pop();
for (int j = 0; j < node->children.size(); j++) {
if (node->children[j]) que.push(node->children[j]);