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