Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5771a93

Browse files
raof01azl397985856
authored andcommitted
1 parent cec4379 commit 5771a93

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

‎problems/104.maximum-depth-of-binary-tree.md‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ var maxDepth = function(root) {
4444

4545
- 队列
4646

47-
- 队列中用 Null(一个特殊元素)来划分每层
47+
- 队列中用 Null(一个特殊元素)来划分每层,或者在对每层进行迭代之前保存当前队列元素的个数(即当前层所含元素个数)
4848

4949
- 树的基本操作- 遍历 - 层次遍历(BFS)
5050

5151
## 代码
52+
* 语言支持:JS,C++
5253

54+
JavaScript Code:
5355
```js
5456
/*
5557
* @lc app=leetcode id=104 lang=javascript
@@ -94,6 +96,40 @@ var maxDepth = function(root) {
9496
return depth;
9597
};
9698
```
99+
C++ Code:
100+
```C++
101+
/**
102+
* Definition for a binary tree node.
103+
* struct TreeNode {
104+
* int val;
105+
* TreeNode *left;
106+
* TreeNode *right;
107+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
108+
* };
109+
*/
110+
class Solution {
111+
public:
112+
int maxDepth(TreeNode* root) {
113+
if (root == nullptr) return 0;
114+
auto q = vector<TreeNode*>();
115+
auto d = 0;
116+
q.push_back(root);
117+
while (!q.empty())
118+
{
119+
++d;
120+
auto sz = q.size();
121+
for (auto i = 0; i < sz; ++i)
122+
{
123+
auto t = q.front();
124+
q.erase(q.begin());
125+
if (t->left != nullptr) q.push_back(t->left);
126+
if (t->right != nullptr) q.push_back(t->right);
127+
}
128+
}
129+
return d;
130+
}
131+
};
132+
```
97133
## 相关题目
98134
- [102.binary-tree-level-order-traversal](./102.binary-tree-level-order-traversal.md)
99-
- [103.binary-tree-zigzag-level-order-traversal](./103.binary-tree-zigzag-level-order-traversal.md)
135+
- [103.binary-tree-zigzag-level-order-traversal](./103.binary-tree-zigzag-level-order-traversal.md)

‎problems/11.container-with-most-water.md‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ eg:
6666

6767

6868
## 代码
69+
* 语言支持:JS,C++
70+
71+
JavaScript Code:
6972

7073
```js
7174
/*
@@ -135,4 +138,19 @@ var maxArea = function(height) {
135138
return max;
136139
};
137140
```
138-
141+
C++ Code:
142+
```C++
143+
class Solution {
144+
public:
145+
int maxArea(vector<int>& height) {
146+
auto ret = 0ul, leftPos = 0ul, rightPos = height.size() - 1;
147+
while( leftPos < rightPos)
148+
{
149+
ret = std::max(ret, std::min(height[leftPos], height[rightPos]) * (rightPos - leftPos));
150+
if (height[leftPos] < height[rightPos]) ++leftPos;
151+
else --rightPos;
152+
}
153+
return ret;
154+
}
155+
};
156+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /