Skip to content

Commit 4af5047

Browse files
update binary tree (#89)
* update binary tree * test if binary trees is same
1 parent 972a831 commit 4af5047

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

datastructures/binarytree/count_left_nodes_of_binary_tree.c renamed to datastructures/binarytree/count_leaf_nodes_of_binary_tree.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ Node *createNode(int data) {
2828
int countLeafNodes(Node *root) {
2929
if (root == NULL) {
3030
return 0;
31-
} else if (root->left == NULL && root->right == NULL) {
31+
}
32+
if (root->left == NULL && root->right == NULL) {
3233
return 1;
33-
} else {
34-
return countLeafNodes(root->left) + countLeafNodes(root->right);
3534
}
35+
return countLeafNodes(root->left) + countLeafNodes(root->right);
3636
}
3737

3838
void test() {

datastructures/binarytree/is_same_binary_tree.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@ Node *createNode(int data) {
3030
bool isSame(Node *root1, Node *root2) {
3131
if (root1 == NULL && root2 == NULL) {
3232
return true;
33-
} else if (root1 != NULL && root2 != NULL) {
34-
if (root1->data == root2->data) {
35-
return isSame(root1->left, root2->left) && isSame(root1->right, root2->right);
36-
} else {
37-
return false;
38-
}
39-
} else {
33+
}
34+
if (root1 == NULL || root2 == NULL) {
35+
return false;
36+
}
37+
if (root1->data != root2->data) {
4038
return false;
4139
}
40+
return isSame(root1->left, root2->left) && isSame(root1->right, root2->right);
4241
}
4342

4443

@@ -61,6 +60,7 @@ void test() {
6160
node5->left = node5->right = NULL;
6261

6362
assert(isSame(root, root));
63+
assert(!isSame(root, NULL));
6464
}
6565

6666
int main() {

0 commit comments

Comments
 (0)