Skip to content

Commit 430b8ae

Browse files
ChunelFengjunfeng.fjyanglbme
committed
更新了55-1,55-2和63题目的Cpp解法 (doocs#338)
* 更新了[25.合并两个排序的链表]和[26.树的子结构]两题的cpp解法 * 更新了[55-1 二叉树的深度][55-2 平衡二叉树][63 股票的最大利润]的cpp解法.以上解法均在leetcode上验证通过,基本双优 * 更新了[25.合并两个排序链表]的信息 Co-authored-by: junfeng.fj <[email protected]> Co-authored-by: Yang Libin <[email protected]>
1 parent e1d19ea commit 430b8ae

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

lcof/面试题55 - I. 二叉树的深度/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ var maxDepth = function (root) {
9494
};
9595
```
9696

97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int maxDepth(TreeNode* root) {
103+
if (nullptr == root) {
104+
return 0;
105+
}
106+
107+
int left = maxDepth(root->left);
108+
int right = maxDepth(root->right);
109+
return std::max(left, right) + 1;
110+
}
111+
};
112+
```
113+
97114
### **...**
98115

99116
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
11+
class Solution {
12+
public:
13+
int maxDepth(TreeNode* root) {
14+
if (nullptr == root) {
15+
return 0;
16+
}
17+
18+
int left = maxDepth(root->left);
19+
int right = maxDepth(root->right);
20+
return std::max(left, right) + 1;
21+
}
22+
};

lcof/面试题55 - II. 平衡二叉树/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,46 @@ function getDepth(node) {
120120
}
121121
```
122122

123+
### **C++**
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
bool isBalanced(TreeNode* root, int* depth) {
129+
if (root == nullptr) {
130+
*depth = 0;
131+
return true;
132+
}
133+
134+
int left = 0;
135+
int right = 0;
136+
if (isBalanced(root->left, &left) && isBalanced(root->right, &right)) {
137+
// 这样做的优势是,不用每次都计算单个子树的深度
138+
int diff = left - right;
139+
if (diff > 1 || diff < -1) {
140+
// 如果有一处不符合 -1 < diff < 1,则直接返回false
141+
return false;
142+
} else {
143+
// 如果符合,则记录当前深度,然后返回上一层继续计算
144+
*depth = max(left, right) + 1;
145+
return true;
146+
}
147+
}
148+
149+
return false; // 如果
150+
}
151+
152+
bool isBalanced(TreeNode* root) {
153+
if (!root) {
154+
return true;
155+
}
156+
157+
int depth = 0;
158+
return isBalanced(root, &depth);
159+
}
160+
};
161+
```
162+
123163
### **...**
124164
125165
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
11+
class Solution {
12+
public:
13+
bool isBalanced(TreeNode* root, int* depth) {
14+
if (root == nullptr) {
15+
*depth = 0;
16+
return true;
17+
}
18+
19+
int left = 0;
20+
int right = 0;
21+
if (isBalanced(root->left, &left)
22+
&& isBalanced(root->right, &right)) {
23+
int diff = left - right;
24+
if (diff > 1 || diff < -1) {
25+
// 如果有一处不符合 -1 < diff < 1,则直接返回false
26+
return false;
27+
} else {
28+
// 如果符合,则记录当前深度,然后返回上一层继续计算
29+
*depth = max(left, right) + 1;
30+
return true;
31+
}
32+
}
33+
34+
return false; // 如果
35+
}
36+
37+
bool isBalanced(TreeNode* root) {
38+
if (!root) {
39+
return true;
40+
}
41+
42+
int depth = 0;
43+
return isBalanced(root, &depth);
44+
}
45+
};

lcof/面试题63. 股票的最大利润/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,37 @@ var maxProfit = function (prices) {
8282
};
8383
```
8484

85+
### **C++**
86+
87+
```cpp
88+
class Solution {
89+
public:
90+
int maxProfit(vector<int>& prices) {
91+
if (prices.size() < 2) {
92+
return 0; // 如果小于两个,直接返回0值
93+
}
94+
95+
int curMin = prices[0];
96+
int maxDiff = prices[1] - prices[0];
97+
98+
// 贪心循环,记录当前最小值,和最大diff值
99+
for (int i = 2; i < prices.size(); i++) {
100+
if (curMin > prices[i-1]) {
101+
curMin = prices[i-1];
102+
}
103+
104+
int diff = prices[i] - curMin;
105+
if (maxDiff < diff) {
106+
maxDiff = diff;
107+
}
108+
}
109+
110+
// 根据题意,如果是负数的话,则返回0值
111+
return maxDiff > 0 ? maxDiff : 0;
112+
}
113+
};
114+
```
115+
85116
### **...**
86117

87118
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices) {
4+
if (prices.size() < 2) {
5+
return 0;
6+
}
7+
8+
int curMin = prices[0];
9+
int maxDiff = prices[1] - prices[0];
10+
11+
for (int i = 2; i < prices.size(); i++) {
12+
if (curMin > prices[i-1]) {
13+
curMin = prices[i-1];
14+
}
15+
16+
int diff = prices[i] - curMin;
17+
if (maxDiff < diff) {
18+
maxDiff = diff;
19+
}
20+
}
21+
22+
return maxDiff > 0 ? maxDiff : 0;
23+
}
24+
};

0 commit comments

Comments
 (0)