-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
156 lines (130 loc) · 3.97 KB
/
main.cpp
File metadata and controls
156 lines (130 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Source: https://leetcode.com/problems/balance-a-binary-search-tree
// Title: Balance a Binary Search Tree
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given the `root` of a binary search tree, return a **balanced** binary search tree with the same node values. If there is more than one answer, return **any of them**.
//
// A binary search tree is **balanced** if the depth of the two subtrees of every node never differs by more than `1`.
//
// **Example 1:**
// https://assets.leetcode.com/uploads/2021/08/10/balance1-tree.jpg
//
// ```
// Input: root = [1,null,2,null,3,null,4,null,null]
// Output: [2,1,3,null,null,null,4]
// Explanation: This is not the only correct answer, [3,1,4,null,2] is also correct.
// ```
//
// **Example 2:**
// https://assets.leetcode.com/uploads/2021/08/10/balanced2-tree.jpg
//
// ```
// Input: root = [2,1,3]
// Output: [2,1,3]
// ```
//
// **Constraints:**
//
// - The number of nodes in the tree is in the range `[1, 10^4]`.
// - `1 <= Node.val <= 10^5`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stack>
#include <utility>
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
// DFS + Divide & Conquer
//
// First convert to array.
// Next build the BST.
class Solution {
// Convert BST to array of node; Inorder DFS.
void bst2arr(TreeNode *root, vector<TreeNode *> &nodes) {
if (root == nullptr) return;
// DFS
bst2arr(root->left, nodes);
nodes.push_back(root);
bst2arr(root->right, nodes);
}
// Build balanced BST from array; Divide and Conquer.
TreeNode *arr2bst(vector<TreeNode *> &nodes, int start, int end) {
if (start >= end) return nullptr;
int mid = start + (end - start) / 2;
auto root = nodes[mid];
root->left = arr2bst(nodes, start, mid);
root->right = arr2bst(nodes, mid + 1, end);
return root;
}
public:
TreeNode *balanceBST(TreeNode *root) {
if (root == nullptr) return nullptr;
auto nodes = vector<TreeNode *>();
bst2arr(root, nodes);
return arr2bst(nodes, 0, nodes.size());
}
};
// DFS + Divide & Conquer
//
// First convert to linked list.
// Next build the BST.
class Solution2 {
struct ListWriter {
int size;
TreeNode dummy;
TreeNode *tail;
ListWriter() : size(0), dummy(), tail(&dummy) {}
TreeNode *head() { return dummy.right; }
void push(TreeNode *node) {
++size;
tail->right = node;
tail = tail->right;
}
};
struct ListReader {
TreeNode *head;
ListReader(TreeNode *head) : head(head) {}
TreeNode *pop() {
if (head == nullptr) return nullptr;
auto node = head;
head = head->right;
return node;
}
};
// Convert BST to linked list; Inorder DFS.
void bst2list(TreeNode *root, ListWriter &writer) {
if (root == nullptr) return;
// DFS
bst2list(root->left, writer);
writer.push(root);
bst2list(root->right, writer);
}
// Build balanced BST from list; Divide and Conquer.
TreeNode *list2bst(int size, ListReader &reader) {
if (size <= 0) return nullptr;
int leftSize = size / 2;
int rightSize = size - leftSize - 1;
TreeNode *left = list2bst(leftSize, reader);
TreeNode *root = reader.pop();
TreeNode *right = list2bst(rightSize, reader);
root->left = left;
root->right = right;
return root;
}
public:
TreeNode *balanceBST(TreeNode *root) {
if (root == nullptr) return nullptr;
auto writer = ListWriter();
bst2list(root, writer);
auto reader = ListReader(writer.head());
return list2bst(writer.size, reader);
}
};