Skip to content

Commit 4082b41

Browse files
committed
tree
1 parent 3ba067a commit 4082b41

3 files changed

+123
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
bool isValidSerialization(char* preorder) {
2+
int node = 0, nullNode = 0, i;
3+
for (i = 0; i < strlen(preorder); i++)
4+
{
5+
if ('#' == preorder[i])
6+
{
7+
nullNode++;
8+
}
9+
else
10+
{
11+
node++;
12+
}
13+
for ( ; i < strlen(preorder); i++)
14+
{
15+
if (',' == preorder[i])
16+
{
17+
break;
18+
}
19+
}
20+
if (nullNode > node && i < strlen(preorder) - 1)
21+
{
22+
return false;
23+
}
24+
}
25+
if (nullNode > node)
26+
{
27+
return true;
28+
}
29+
else
30+
{
31+
return false;
32+
}
33+
}

450-Delete-Node-in-a-BST.c

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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* deleteNode(struct TreeNode* root, int key) {
10+
if (NULL == root)
11+
{
12+
return root;
13+
}
14+
if (root->val > key)
15+
{
16+
root->left = deleteNode(root->left, key);
17+
}
18+
else if (root->val < key)
19+
{
20+
root->right = deleteNode(root->right, key);
21+
}
22+
else
23+
{
24+
if (NULL == root->left)
25+
{
26+
return root->right;
27+
}
28+
else if (NULL == root->right)
29+
{
30+
return root->left;
31+
}
32+
else
33+
{
34+
struct TreeNode *temp = root->right;
35+
while (NULL != temp->left)
36+
{
37+
temp = temp->left;
38+
}
39+
root->val = temp->val;
40+
root->right = deleteNode(root->right, temp->val);
41+
}
42+
}
43+
return root;
44+
}

543-Diameter-of-Binary-Tree.c

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 max(int a, int b)
10+
{
11+
return a > b ? a : b;
12+
}
13+
int height(struct TreeNode *root)
14+
{
15+
if (NULL == root)
16+
{
17+
return -1;
18+
}
19+
return root->val;
20+
}
21+
int diameterOfBinaryTree(struct TreeNode* root) {
22+
if (NULL == root)
23+
{
24+
return 0;
25+
}
26+
int lenth2 = diameterOfBinaryTree(root->left);
27+
int lenth3 = diameterOfBinaryTree(root->right);
28+
int leftHeight = height(root->left);
29+
int rightHeight = height(root->right);
30+
int lenth1 = 2 + leftHeight + rightHeight;
31+
root->val = max(leftHeight, rightHeight) + 1;
32+
int max;
33+
if (lenth1 > lenth2)
34+
{
35+
max = lenth1;
36+
}
37+
else
38+
{
39+
max = lenth2;
40+
}
41+
if (max < lenth3)
42+
{
43+
max = lenth3;
44+
}
45+
return max;
46+
}

0 commit comments

Comments
 (0)