Skip to content

Commit f18c98e

Browse files
committed
0998 added.
1 parent f4655e3 commit f18c98e

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
project(C)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(C main.cpp)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/// Source : https://leetcode.com/problems/maximum-binary-tree-ii/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-23
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Definition for a binary tree node.
12+
struct TreeNode {
13+
int val;
14+
TreeNode *left;
15+
TreeNode *right;
16+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
17+
};
18+
19+
20+
/// Simulation
21+
/// Time Complexity: O(n^2)
22+
/// Space Complexity: O(n)
23+
class Solution {
24+
public:
25+
TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
26+
27+
vector<int> A;
28+
dfs(root, A);
29+
A.push_back(val);
30+
return construct(A, 0, A.size() - 1);
31+
}
32+
33+
private:
34+
TreeNode* construct(const vector<int>& A, int l, int r){
35+
36+
if(l > r) return NULL;
37+
38+
int best_val = A[l], best_index = l;
39+
for(int i = l + 1; i <= r; i ++)
40+
if(A[i] > best_val)
41+
best_val = A[i], best_index = i;
42+
43+
TreeNode* retNode = new TreeNode(A[best_index]);
44+
retNode->left = construct(A, l, best_index - 1);
45+
retNode->right = construct(A, best_index + 1, r);
46+
return retNode;
47+
}
48+
49+
void dfs(TreeNode* root, vector<int>& A){
50+
51+
if(!root) return;
52+
dfs(root->left, A);
53+
A.push_back(root->val);
54+
dfs(root->right, A);
55+
}
56+
};
57+
58+
int main() {
59+
60+
return 0;
61+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
666666
| 995 | [Minimum Number of K Consecutive Bit Flips](https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/) | [solution](https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/solution/) | [C++](0995-Minimum-Number-of-K-Consecutive-Bit-Flips/cpp-0995/) | | |
667667
| 996 | [Number of Squareful Arrays](https://leetcode.com/problems/number-of-squareful-arrays/) | [solution](https://leetcode.com/problems/number-of-squareful-arrays/solution/) | [C++](0996-Number-of-Squareful-Arrays/cpp-0996/) | | |
668668
| 997 | [Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/) | [] | [C++](0997-Find-the-Town-Judge/cpp-0997/) | | |
669+
| 998 | [Maximum Binary Tree II](https://leetcode.com/problems/maximum-binary-tree-ii/) | [] | [C++](0998-Maximum-Binary-Tree-II/cpp-0998/) | | |
669670
| | | | | | |
670671
| 1000 | [Minimum Cost to Merge Stones](https://leetcode.com/problems/minimum-cost-to-merge-stones/) | [] | [C++](1000-Minimum-Cost-to-Merge-Stones/cpp-1000/) | | |
671672
| | | | | | |

0 commit comments

Comments
 (0)