Skip to content

Commit fdc1242

Browse files
committed
tree
1 parent 86fa354 commit fdc1242

9 files changed

+307
-0
lines changed

100-Same-Tree.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
10+
if ((NULL == p && NULL != q) || (NULL != p && NULL == q))
11+
{
12+
return false;
13+
}
14+
if (NULL == p && NULL == q)
15+
{
16+
return true;
17+
}
18+
return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
19+
}

101-Symmetric-Tree.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
bool mirror(struct TreeNode *p, struct TreeNode *q)
10+
{
11+
if ((NULL == p && NULL != q) || (NULL != p && NULL == q))
12+
{
13+
return false;
14+
}
15+
if (NULL == p && NULL == q)
16+
{
17+
return true;
18+
}
19+
return p->val == q->val && mirror(p->left, q->right) && mirror(p->right, q->left);
20+
}
21+
22+
bool isSymmetric(struct TreeNode* root) {
23+
if (NULL == root)
24+
{
25+
return true;
26+
}
27+
return mirror(root->left, root->right);
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize) {
10+
if (preorderSize <= 0)
11+
{
12+
return NULL;
13+
}
14+
struct TreeNode *newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
15+
newNode->val = preorder[0];
16+
int i;
17+
for (i = 0; i < inorderSize; i++)
18+
{
19+
if (preorder[0] == inorder[i])
20+
{
21+
break;
22+
}
23+
}
24+
newNode->left = buildTree(preorder + 1, i, inorder, i);
25+
newNode->right = buildTree(preorder + i + 1, preorderSize - i - 1, inorder + i + 1, preorderSize - i - 1);
26+
return newNode;
27+
}

145-Binary-Tree-Postorder-Traversal.c

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
* Return an array of size *returnSize.
11+
* Note: The returned array must be malloced, assume caller calls free().
12+
*/
13+
int* postorderTraversal(struct TreeNode* root, int* returnSize) {
14+
int *result = (int*)malloc(1000000 * sizeof(int));
15+
struct TreeNode* stack[100000];
16+
struct TreeNode *visit = NULL;
17+
int top = -1;
18+
*returnSize = 0;
19+
while (NULL != root || -1 != top)
20+
{
21+
while (NULL != root)
22+
{
23+
stack[++top] = root;
24+
root = root->left;
25+
}
26+
root = stack[top];
27+
if (root->right == visit || NULL == root->right)
28+
{
29+
result[(*returnSize)++] = root->val;
30+
top--;
31+
visit = root;
32+
root = NULL;
33+
}
34+
else
35+
{
36+
root = root->right;
37+
}
38+
}
39+
return result;
40+
}

199-Binary-Tree-Right-Side-View.c

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
* Return an array of size *returnSize.
11+
* Note: The returned array must be malloced, assume caller calls free().
12+
*/
13+
int* rightSideView(struct TreeNode* root, int* returnSize) {
14+
int *result = (int*)malloc(100000 * sizeof(int));
15+
struct TreeNode* queue[100000];
16+
int head = 0, tail = -1, sentinel = 0;
17+
*returnSize = 0;
18+
if (NULL != root)
19+
{
20+
queue[++tail] = root;
21+
}
22+
while (head <= tail)
23+
{
24+
if (NULL != queue[head]->left)
25+
{
26+
queue[++tail] = queue[head]->left;
27+
}
28+
if (NULL != queue[head]->right)
29+
{
30+
queue[++tail] = queue[head]->right;
31+
}
32+
if (head == sentinel)
33+
{
34+
result[(*returnSize)++] = queue[head]->val;
35+
sentinel = tail;
36+
}
37+
head++;
38+
}
39+
return result;
40+
}
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+
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
10+
if (NULL == root)
11+
{
12+
return NULL;
13+
}
14+
if (p == root)
15+
{
16+
return p;
17+
}
18+
else if (q == root)
19+
{
20+
return q;
21+
}
22+
struct TreeNode *left, *right;
23+
left = lowestCommonAncestor(root->left, p, q);
24+
right = lowestCommonAncestor(root->right, p, q);
25+
if (NULL == left)
26+
{
27+
return right;
28+
}
29+
else if (NULL == right)
30+
{
31+
return left;
32+
}
33+
else
34+
{
35+
return root;
36+
}
37+
}

513-Find-Bottom-Left-Tree-Value.c

+37
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+
int findBottomLeftValue(struct TreeNode* root) {
10+
int result[10000];
11+
struct TreeNode* queue[100000];
12+
int head = 0, tail = -1, sentinel = 0;
13+
int i = 0;
14+
queue[++tail] = root;
15+
result[i++] = root->val;
16+
while (head <= tail)
17+
{
18+
if (NULL != queue[head]->left)
19+
{
20+
queue[++tail] = queue[head]->left;
21+
}
22+
if (NULL != queue[head]->right)
23+
{
24+
queue[++tail] = queue[head]->right;
25+
}
26+
if (head == sentinel)
27+
{
28+
if (head < tail)
29+
{
30+
result[i++] = queue[head + 1]->val;
31+
}
32+
sentinel = tail;
33+
}
34+
head++;
35+
}
36+
return result[i - 1];
37+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
* Return an array of size *returnSize.
11+
* Note: The returned array must be malloced, assume caller calls free().
12+
*/
13+
int* largestValues(struct TreeNode* root, int* returnSize) {
14+
int *result = (int*)malloc(100000 * sizeof(int));
15+
struct TreeNode* queue[100000];
16+
int head = 0, tail = -1, sentinel = 0;
17+
int max;
18+
*returnSize = 0;
19+
if (NULL != root)
20+
{
21+
queue[++tail] = root;
22+
max = queue[head]->val;
23+
}
24+
while (head <= tail)
25+
{
26+
if (NULL != queue[head]->left)
27+
{
28+
queue[++tail] = queue[head]->left;
29+
}
30+
if (NULL != queue[head]->right)
31+
{
32+
queue[++tail] = queue[head]->right;
33+
}
34+
if (max < queue[head]->val)
35+
{
36+
max = queue[head]->val;
37+
}
38+
if (head == sentinel)
39+
{
40+
result[(*returnSize)++] = max;
41+
sentinel = tail;
42+
if (head < tail)
43+
{
44+
max = queue[head + 1]->val;
45+
}
46+
}
47+
head++;
48+
}
49+
return result;
50+
}

94-Binary-Tree-Inorder-Traversal.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
* Return an array of size *returnSize.
11+
* Note: The returned array must be malloced, assume caller calls free().
12+
*/
13+
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
14+
int *result = (int*)malloc(1000000 * sizeof(int));
15+
struct TreeNode* stack[100000];
16+
int top = -1;
17+
*returnSize = 0;
18+
while (NULL != root || -1 != top)
19+
{
20+
while (NULL != root)
21+
{
22+
stack[++top] = root;
23+
root = root->left;
24+
}
25+
result[(*returnSize)++] = stack[top]->val;
26+
root = stack[top--]->right;
27+
}
28+
return result;
29+
}

0 commit comments

Comments
 (0)