Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help! ``` /* * Your task is to implement 'levelsum' * which returns the sum of all the nodes at a particular level in

Please help!image text in transcribed

```

/* * Your task is to implement 'levelsum' * which returns the sum of all the nodes at a particular level in * the binary tree 'root' */ #include  #include  struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; struct TreeNode * createTreeNode(int val); void destroyTreeNode(struct TreeNode * node); void displayTreeNode(struct TreeNode * node); struct TreeNode * buildTree(); void displayInorder(struct TreeNode * root); void displayPreorder(struct TreeNode * root); int levelsum(struct TreeNode * root, int level); void main() { struct TreeNode * root = buildTree(); printf("Inorder: "); displayInorder(root); printf(" "); printf("Preorder: "); displayPreorder(root); printf(" "); printf("Level %d: =%d ", 0, levelsum(root, 0)); printf("Level %d: =%d ", 1, levelsum(root, 1)); printf("Level %d: =%d ", 2, levelsum(root, 2)); printf("Level %d: =%d ", 3, levelsum(root, 3)); } /* * The following function doesn't work, so your task is to * make it work */ int levelsum(struct TreeNode * root, int level) { return 0; } struct TreeNode * createTreeNode(int val) { struct TreeNode * node = (struct TreeNode *) malloc(sizeof(struct TreeNode)); node->val = val; node->left = NULL; node->right = NULL; return node; } void destroyTreeNode(struct TreeNode * node) { free(node); } void displayTreeNode(struct TreeNode * node) { printf("%d", node->val); } struct TreeNode * buildTree() { struct TreeNode *p0 = createTreeNode(0); struct TreeNode *p1 = createTreeNode(1); struct TreeNode *p2 = createTreeNode(2); struct TreeNode *p3 = createTreeNode(3); struct TreeNode *p4 = createTreeNode(4); struct TreeNode *p5 = createTreeNode(5); struct TreeNode *p6 = createTreeNode(6); struct TreeNode *p7 = createTreeNode(7); struct TreeNode *p8 = createTreeNode(8); p3->left = p5; p3->right = p1; p5->left = p6; p5->right = p2; p2->left = p7; p2->right = p4; p1->left = p0; p1->right = p8; return p3; } void displayInorder(struct TreeNode * root) { if (root == NULL) return; displayInorder(root->left); displayTreeNode(root); printf(" "); displayInorder(root->right); } void displayPreorder(struct TreeNode * root) { if (root == NULL) return; displayTreeNode(root); printf(" "); displayPreorder(root->left); displayPreorder(root->right); } ``` 
Problem 2 [1 pt]. Given a binary tree, sum the values in nodes at the same 'level', where a level of a node is the distance from the node to the root. For example, the root is at level 0 , and its children are at level 1. In the example below, the sum at level 0 is 3 ; the sum at level 1 is 6; the sum at level 2 is 16 ; and the sum at level 3 is 11 . int levelsum(struct TreeNode * root, int level); which returns the sum at level 'level' for the tree 'root'. The main() program will display the sum. Note that the tree that is built in the program is shown in the figure above. The current version of 'Levelsum( )' doesn't work, so make it work by only modifying 'levelsum( )

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

2. What are the components of IT infrastructure?

Answered: 1 week ago