File tree Expand file tree Collapse file tree 6 files changed +179
-0
lines changed Expand file tree Collapse file tree 6 files changed +179
-0
lines changed Original file line number Diff line number Diff 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 number Diff line number Diff line change 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+ };
Original file line number Diff line number Diff 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 number Diff line number Diff line change 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+ };
Original file line number Diff line number Diff 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 number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments