Skip to content

Commit f9f78a4

Browse files
committed
solved 5 problems.
1 parent 84e7630 commit f9f78a4

File tree

5 files changed

+87
-0
lines changed
  • 107.Binary Tree Level Order Traversal II/coderfive
  • 110.Balanced Binary Tree/coderfive
  • 111.Minimum Depth of Binary Tree/coderfive

5 files changed

+87
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> levelOrderBottom(TreeNode* root) {
4+
vector<vector<int>> res;
5+
vector<TreeNode*> v, v2, *p = &v, *p2 = &v2;
6+
if (!root) return res;
7+
v.push_back(root);
8+
while (p->size() != 0) {
9+
vector<int> vi;
10+
for (int i = 0; i < p->size(); i++) {
11+
vi.push_back((*p)[i]->val);
12+
if ((*p)[i]->left) p2->push_back((*p)[i]->left);
13+
if ((*p)[i]->right) p2->push_back((*p)[i]->right);
14+
}
15+
res.push_back(std::move(vi));
16+
std::swap (p, p2);
17+
p2->clear();
18+
}
19+
std::reverse(res.begin(), res.end());
20+
21+
return res;
22+
}
23+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
TreeNode* sortedArrayToBST(vector<int>& nums) {
4+
return toBST (nums, 0, nums.size());
5+
}
6+
TreeNode* toBST (const vector<int>& v, int b, int e) {
7+
if (b == e) return NULL;
8+
int mid = b + (e-b)/2;
9+
TreeNode* node = new TreeNode(v[mid]);
10+
node->left = toBST(v, b, mid);
11+
node->right = toBST(v, mid+1, e);
12+
return node;
13+
}
14+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
TreeNode* sortedListToBST(ListNode* head) {
4+
int n = 0;
5+
ListNode* p = head;
6+
while (head) {
7+
n++;
8+
head = head->next;
9+
}
10+
return toBST(&p, n);
11+
}
12+
TreeNode* toBST (ListNode** head, int n) {
13+
int left_size = n/2;
14+
if (n == 0) return NULL;
15+
TreeNode* left = toBST(head, left_size);
16+
TreeNode* res = new TreeNode((*head)->val);
17+
*head = (*head)->next;
18+
res->left = left;
19+
res->right = toBST(head, n-left_size-1);
20+
21+
return res;
22+
}
23+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
bool isBalanced(TreeNode* root) {
4+
int a = 0;
5+
return is_b (root, a);
6+
}
7+
bool is_b (TreeNode* root, int& depth) {
8+
if (!root) return true;
9+
int a = 0, b = 0;
10+
if (!is_b(root->left, a)) return false;
11+
if (!is_b(root->right, b)) return false;
12+
if (a < b) std::swap(a, b);
13+
if (a-b > 1) return false;
14+
depth += a+1;
15+
return true;
16+
}
17+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int minDepth(TreeNode* root) {
4+
if (!root) return 0;
5+
int left = minDepth(root->left);
6+
int right = minDepth(root->right);
7+
if (left == 0 || right == 0) return 1+left+right;
8+
return 1 + std::min(left, right);
9+
}
10+
};

0 commit comments

Comments
 (0)