Skip to content

Commit 8923076

Browse files
committed
Tree & 20230506 daily chanllenge
1 parent 67d6dde commit 8923076

14 files changed

+674
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @lc app=leetcode id=104 lang=cpp
3+
*
4+
* [104] Maximum Depth of Binary Tree
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
17+
* };
18+
*/
19+
class Solution {
20+
public:
21+
int maxDepth(TreeNode* root) {
22+
if(root==NULL) return 0;
23+
int lh=maxDepth(root->left);
24+
int rh=maxDepth(root->right);
25+
return 1+max(lh,rh);
26+
}
27+
};
28+
// @lc code=end
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @lc app=leetcode id=105 lang=cpp
3+
*
4+
* [105] Construct Binary Tree from Preorder and Inorder Traversal
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
17+
* };
18+
*/
19+
class Solution {
20+
unordered_map<int, int> valToIndex;
21+
public:
22+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
23+
for (int i = 0; i < inorder.size(); i++) {
24+
valToIndex[inorder[i]] = i;
25+
}
26+
return build(preorder, 0, preorder.size() - 1,
27+
inorder, 0, inorder.size() - 1);
28+
}
29+
30+
TreeNode* build(vector<int>& preorder, int preStart, int preEnd,
31+
vector<int>& inorder, int inStart, int inEnd) {
32+
if (preStart > preEnd) {
33+
return nullptr;
34+
}
35+
36+
// root 节点对应的值就是前序遍历数组的第一个元素
37+
int rootVal = preorder[preStart];
38+
// rootVal 在中序遍历数组中的索引
39+
int index = valToIndex[rootVal];
40+
41+
int leftSize = index - inStart;
42+
43+
// 先构造出当前根节点
44+
TreeNode* root = new TreeNode(rootVal);
45+
// 递归构造左右子树
46+
root->left = build(preorder, preStart + 1, preStart + leftSize,
47+
inorder, inStart, index - 1);
48+
49+
root->right = build(preorder, preStart + leftSize + 1, preEnd,
50+
inorder, index + 1, inEnd);
51+
return root;
52+
}
53+
};
54+
// @lc code=end
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @lc app=leetcode id=114 lang=cpp
3+
*
4+
* [114] Flatten Binary Tree to Linked List
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
17+
* };
18+
*/
19+
class Solution {
20+
public:
21+
void flatten(TreeNode* root) {
22+
// base case
23+
if (root == nullptr) return;
24+
25+
// 利用定义,把左右子树拉平
26+
flatten(root->left);
27+
flatten(root->right);
28+
29+
/**** 后序遍历位置 ****/
30+
// 1、左右子树已经被拉平成一条链表
31+
TreeNode* left = root->left;
32+
TreeNode* right = root->right;
33+
34+
// 2、将左子树作为右子树
35+
root->left = nullptr;
36+
root->right = left;
37+
38+
// 3、将原先的右子树接到当前右子树的末端
39+
TreeNode* p = root;
40+
while (p->right != nullptr) {
41+
p = p->right;
42+
}
43+
p->right = right;
44+
}
45+
};
46+
// @lc code=end
47+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* @lc app=leetcode id=116 lang=cpp
3+
*
4+
* [116] Populating Next Right Pointers in Each Node
5+
*/
6+
7+
// @lc code=start
8+
/*
9+
// Definition for a Node.
10+
class Node {
11+
public:
12+
int val;
13+
Node* left;
14+
Node* right;
15+
Node* next;
16+
17+
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
18+
19+
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
20+
21+
Node(int _val, Node* _left, Node* _right, Node* _next)
22+
: val(_val), left(_left), right(_right), next(_next) {}
23+
};
24+
*/
25+
26+
class Solution {
27+
public:
28+
Node* connect(Node* root) {
29+
if (root == nullptr) return nullptr;
30+
// 遍历「三叉树」,连接相邻节点
31+
traverse(root->left, root->right);
32+
return root;
33+
}
34+
35+
// 三叉树遍历框架
36+
void traverse(Node* node1, Node* node2) {
37+
if (node1 == nullptr || node2 == nullptr) {
38+
return;
39+
}
40+
/**** 前序位置 ****/
41+
// 将传入的两个节点穿起来
42+
node1->next = node2;
43+
44+
// 连接相同父节点的两个子节点
45+
traverse(node1->left, node1->right);
46+
traverse(node2->left, node2->right);
47+
// 连接跨越父节点的两个子节点
48+
traverse(node1->right, node2->left);
49+
}
50+
};
51+
// @lc code=end
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @lc app=leetcode id=144 lang=cpp
3+
*
4+
* [144] Binary Tree Preorder Traversal
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
17+
* };
18+
*/
19+
class Solution {
20+
vector<int> ans;
21+
public:
22+
vector<int> preorderTraversal(TreeNode* root) {
23+
if (root) {
24+
ans.push_back(root->val);
25+
preorderTraversal(root->left);
26+
preorderTraversal(root->right);
27+
}
28+
return ans;
29+
}
30+
};
31+
// @lc code=end
32+

Tree/226.invert-binary-tree.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @lc app=leetcode id=226 lang=cpp
3+
*
4+
* [226] Invert Binary Tree
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
17+
* };
18+
*/
19+
class Solution {
20+
public:
21+
TreeNode* invertTree(TreeNode* root) {
22+
if (root == nullptr) {
23+
return nullptr;
24+
}
25+
// 利用函数定义,先翻转左右子树
26+
TreeNode* left = invertTree(root->left);
27+
TreeNode* right = invertTree(root->right);
28+
29+
// 然后交换左右子节点
30+
root->left = right;
31+
root->right = left;
32+
33+
// 和定义逻辑自恰:以 root 为根的这棵二叉树已经被翻转,返回 root
34+
return root;
35+
}
36+
};
37+
// @lc code=end
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* @lc app=leetcode id=297 lang=cpp
3+
*
4+
* [297] Serialize and Deserialize Binary Tree
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
15+
* };
16+
*/
17+
class Codec {
18+
public:
19+
20+
string serialize(TreeNode* root) {
21+
ostringstream out;
22+
serialize(root, out);
23+
return out.str();
24+
}
25+
26+
TreeNode* deserialize(string data) {
27+
istringstream in(data);
28+
return deserialize(in);
29+
}
30+
31+
private:
32+
33+
void serialize(TreeNode* root, ostringstream& out) {
34+
if (root) {
35+
out << root->val << ' ';
36+
serialize(root->left, out);
37+
serialize(root->right, out);
38+
} else {
39+
out << "# ";
40+
}
41+
}
42+
43+
TreeNode* deserialize(istringstream& in) {
44+
string val;
45+
in >> val;
46+
if (val == "#")
47+
return nullptr;
48+
TreeNode* root = new TreeNode(stoi(val));
49+
root->left = deserialize(in);
50+
root->right = deserialize(in);
51+
return root;
52+
}
53+
};
54+
55+
// Your Codec object will be instantiated and called as such:
56+
// Codec ser, deser;
57+
// TreeNode* ans = deser.deserialize(ser.serialize(root));
58+
// @lc code=end
59+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* @lc app=leetcode id=315 lang=cpp
3+
*
4+
* [315] Count of Smaller Numbers After Self
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
struct Pair {
11+
int val, id;
12+
Pair(int val, int id) {
13+
// 记录数组的元素值
14+
this->val = val;
15+
// 记录元素在数组中的原始索引
16+
this->id = id;
17+
}
18+
};
19+
20+
// 归并排序所用的辅助数组
21+
Pair* temp;
22+
// 记录每个元素后面比自己小的元素个数
23+
int* count;
24+
25+
// 主函数
26+
vector<int> countSmaller(vector<int>& nums) {
27+
int n = nums.size();
28+
count = new int[n]();
29+
temp = new Pair[n]();
30+
Pair* arr = new Pair[n];
31+
// 记录元素原始的索引位置,以便在 count 数组中更新结果
32+
for (int i = 0; i < n; i++)
33+
arr[i] = Pair(nums[i], i);
34+
35+
// 执行归并排序,本题结果被记录在 count 数组中
36+
sort(arr, 0, n - 1);
37+
38+
vector<int> res;
39+
for (int i = 0; i < n; i++)
40+
res.push_back(count[i]);
41+
delete[] count;
42+
delete[] temp;
43+
return res;
44+
}
45+
46+
// 归并排序
47+
void sort(Pair* arr, int lo, int hi) {
48+
if (lo == hi) return;
49+
int mid = lo + (hi - lo) / 2;
50+
sort(arr, lo, mid);
51+
sort(arr, mid + 1, hi);
52+
merge(arr, lo, mid, hi);
53+
}
54+
55+
// 合并两个有序数组
56+
void merge(Pair* arr, int lo, int mid, int hi) {
57+
for (int i = lo; i <= hi; i++) {
58+
temp[i] = arr[i];
59+
}
60+
61+
int i = lo, j = mid + 1;
62+
for (int p = lo; p <= hi; p++) {
63+
if (i == mid + 1) {
64+
arr[p] = temp[j++];
65+
} else if (j == hi + 1) {
66+
arr[p] = temp[i++];
67+
// 更新 count 数组
68+
count[arr[p].id] += j - mid - 1;
69+
} else if (temp[i].val > temp[j].val) {
70+
arr[p] = temp[j++];
71+
} else {
72+
arr[p] = temp[i++];
73+
// 更新 count 数组
74+
count[arr[p].id] += j - mid - 1;
75+
}
76+
}
77+
}
78+
};
79+
// @lc code=end
80+

0 commit comments

Comments
 (0)