mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1995 from ayao98/master
更新 0104.二叉树的最大深度 0113.路径综合 C++函数命名有错误
This commit is contained in:
@ -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]);
|
||||
|
@ -250,7 +250,7 @@ private:
|
||||
vector<vector<int>> result;
|
||||
vector<int> path;
|
||||
// 递归函数不需要返回值,因为我们要遍历整个树
|
||||
void traversal(treenode* cur, int count) {
|
||||
void traversal(TreeNode* cur, int count) {
|
||||
if (!cur->left && !cur->right && count == 0) { // 遇到了叶子节点且找到了和为sum的路径
|
||||
result.push_back(path);
|
||||
return;
|
||||
@ -276,10 +276,10 @@ private:
|
||||
}
|
||||
|
||||
public:
|
||||
vector<vector<int>> pathsum(treenode* root, int sum) {
|
||||
vector<vector<int>> pathSum(TreeNode* root, int sum) {
|
||||
result.clear();
|
||||
path.clear();
|
||||
if (root == null) return result;
|
||||
if (root == NULL) return result;
|
||||
path.push_back(root->val); // 把根节点放进路径
|
||||
traversal(root, sum - root->val);
|
||||
return result;
|
||||
|
Reference in New Issue
Block a user