Skip to content

Commit 62a5cf4

Browse files
committed
sol 114, 116, 222 and 243
1 parent c8c5c82 commit 62a5cf4

File tree

6 files changed

+218
-1
lines changed

6 files changed

+218
-1
lines changed

c_code/114_flatten_1.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
11+
// 递归写法
12+
void flatten(struct TreeNode* root)
13+
{
14+
if (root == NULL) return;
15+
16+
// 先得到两个flatten后的左右子树
17+
flatten(root->left);
18+
flatten(root->right);
19+
20+
// 重新接上左右子树
21+
struct TreeNode* left = root->left;
22+
struct TreeNode* right = root->right;
23+
24+
// 接上左子树
25+
root->left = NULL;
26+
root->right = left;
27+
28+
// 找到原左子树flatteng的末尾节点
29+
struct TreeNode* p = root; //直接从root开始赋值,比root->right赋值要好,省去一个NULL判断
30+
while (p->right != NULL) {
31+
p = p->right;
32+
}
33+
// 接上原右子树
34+
p->right = right;
35+
36+
return;
37+
}

c_code/114_flatten_2.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
// 自己的方法,利用先序方法重新建立个树,调试通过
11+
12+
struct TreeNode {
13+
int val;
14+
struct TreeNode *left;
15+
struct TreeNode *right;
16+
};
17+
18+
void firstTraverse1(struct TreeNode* root)
19+
{
20+
if (root == NULL) return;
21+
22+
printf("%d ", root->val);
23+
firstTraverse1(root->left);
24+
firstTraverse1(root->right);
25+
return;
26+
}
27+
28+
void firstTraverse2(struct TreeNode* root)
29+
{
30+
if (root == NULL) return;
31+
32+
printf("%d ", root->val);
33+
firstTraverse2(root->right);
34+
return;
35+
}
36+
37+
38+
struct TreeNode* g_pre;
39+
void firstFlatten(struct TreeNode* root)
40+
{
41+
if (root == NULL) return;
42+
43+
// 先生成一个节点
44+
struct TreeNode* node = (struct TreeNode *)malloc(sizeof(struct TreeNode));
45+
node->val = root->val; // copy root
46+
node->left = NULL;
47+
g_pre->right = node;
48+
g_pre = node;
49+
50+
firstFlatten(root->left);
51+
firstFlatten(root->right);
52+
53+
return;
54+
}
55+
56+
// 自己的方法,利用先序遍历来连接节点
57+
void flatten(struct TreeNode* root)
58+
{
59+
struct TreeNode* newRoot = (struct TreeNode *)malloc(sizeof(struct TreeNode));
60+
newRoot->val = -1;
61+
newRoot->left = NULL;
62+
newRoot->right = NULL;
63+
g_pre = newRoot;
64+
65+
firstTraverse1(root); // left and right
66+
printf("\n");
67+
68+
firstFlatten(root);
69+
root = newRoot->right; // 缺点是,由于接口限制,单独创立的next副本无法返回;
70+
g_pre->right = NULL; // 处理完后,所有节点后,最末尾的节点设为NULL
71+
72+
firstTraverse2(root); // only right
73+
74+
return;
75+
}
76+
77+
78+
int main(void)
79+
{
80+
struct TreeNode n1, n2, n3, n4, n5, n6;
81+
n1.val = 1;
82+
n2.val = 5;
83+
n3.val = 6;
84+
n4.val = 7;
85+
n5.val = 8;
86+
n6.val = 9;
87+
n1.left = &n2;
88+
n1.right = &n5;
89+
n2.left = &n3;
90+
n2.right = &n4;
91+
n3.left = NULL;
92+
n3.right = NULL;
93+
n4.left = NULL;
94+
n4.right = NULL;
95+
n5.left = NULL;
96+
n5.right = &n6;
97+
n6.left = NULL;
98+
n6.right = NULL;
99+
100+
flatten(&n1);
101+
102+
// while (1) { ; }
103+
104+
return 0;
105+
}

c_code/116_connect.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a Node.
3+
* struct Node {
4+
* int val;
5+
* struct Node *left;
6+
* struct Node *right;
7+
* struct Node *next;
8+
* };
9+
*/
10+
11+
// 参考: 此题不熟,需要多练
12+
// https://labuladong.gitbook.io/algo/shu-ju-jie-gou-xi-lie/shou-ba-shou-shua-er-cha-shu-xun-lian-di-gui-si-wei/er-cha-shu-xi-lie-1
13+
14+
void connectTwoNodes(struct Node* node1, struct Node* node2)
15+
{
16+
if (node1 == NULL || node2 == NULL) return;
17+
18+
// 连接左右两个节点
19+
node1->next = node2;
20+
21+
// 连接同一父节点下两个节点
22+
connectTwoNodes(node1->left, node1->right);
23+
connectTwoNodes(node2->left, node2->right);
24+
// 连接不同父节点下两个相邻节点
25+
connectTwoNodes(node1->right, node2->left);
26+
}
27+
28+
struct Node* connect(struct Node* root) {
29+
if (root == NULL) return root;
30+
31+
connectTwoNodes(root->left, root->right);
32+
33+
return root;
34+
}

c_code/222_countNodes.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
11+
int countNodes(struct TreeNode* root){ // 可计算任意二叉树节点总数(包含完全二叉树(满二叉树))
12+
if (root == NULL) return 0;
13+
14+
// 满二叉树
15+
int hl = 1, hr = 1;
16+
struct TreeNode* l = root->left;
17+
struct TreeNode* r = root->right;
18+
while (l != NULL) { // 右子树高度
19+
l = l->left;
20+
hl++;
21+
}
22+
while (r != NULL) { //左子树高度
23+
r = r->right;
24+
hr++;
25+
}
26+
if (hl == hr) { // 若为满二叉树
27+
return pow(2, hl) - 1; // 满二叉树节点计算:2^depth - 1
28+
}
29+
30+
// 普通二叉树
31+
return 1 + countNodes(root->left) + countNodes(root->right);
32+
}
File renamed without changes.

c_code/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,13 @@
1919
**LeetCode:二叉树类递归题** <br>
2020
226.翻转二叉树(简单)
2121
114.二叉树展开为链表(中等)
22-
116.填充每个节点的下一个右侧节点指针(中等)
22+
116.填充每个节点的下一个右侧节点指针(中等) || 此题不熟,需要多练
23+
226
24+
654
25+
105
26+
106
27+
222. 完全二叉树的节点个数
28+
29+
30+
# 二分法思路训练题目搜集
31+
34. 在排序数组中查找元素的第一个和最后一个位置

0 commit comments

Comments
 (0)