Choose the appropriate running time from the list below.
The variable
Perform a post-order traversal of a Binary Tree.
Recently, your TA team has had a bit of wanderlust. They've written down a few places they'd like to go, and, as all TAs do, chosen one and created a binary tree! They're too busy daydreaming to write down all of its traversals, so they need your help.
Given the following binary tree, provide all four traversals. Your solution should contain only letters -- no commas or brackets!
RKKAAIOKKAKARIKOKAAKIOKRRKAKAKIOConsider the binary tree class described in lecture where we have 1) variable root that is the treeNode pointer representing the root of the binary tree and 2) each treeNode consists of an integer data element, and two treeNode pointers called left and right.
int fun (treeNode* curr) {
if (curr != NULL) {
ret1 = fun(curr->right);
ret2 = fun(curr->left);
return 1 + max(ret1, ret2);
}
else return -1;
}
What does fun(root) return?
fun returns the number of elements in the tree.fun returns the shortest distance from root to leaf.fun returns the height of the tree.fun returns the sum of all elements in the tree.It's 1AM, and all through the house, not a creature was stirring... except for your TA. They've been given the In-Order and Post-Order traversals of a binary tree, but they're missing the Pre-Order and Level-Order traversals...
they're tired... so tired... and then they realized that they could ask their students to help them out!
Given the following In-Order and Post-Order traversals of a binary tree:
find the Pre-Order and Level-Order traversal.
HDKNOLMCHDOKLMNCChoose the appropriate worst-case expression from the list below.
The variable
In a perfect binary tree, what is the length of the longest path from an arbitrary node
Consider this binary tree Node structure:
struct Node {
int key;
int value;
Node *left, *right;
Node(int key_, int value_ = 0, Node *left_ = nullptr, Node *right_ = nullptr) :
key(key_), value(value_), left(left_), right(right_) {}
};
Complete the function void waldo(Node *¤t) below. current is part of (and maybe the overall root of) a binary tree in which there are no duplicate keys and no duplicate values. waldo performs pre-order traversal of current's subtree, where the action in the traversal is: Check if the current node has a non-null left child whose value is smaller than the current node's value and, if so, swap the positions of the two nodes in the tree.
NOTE:
current is passed by reference. If it ends up being reassigned, be sure to update the reference!#include <cstdlib> // in case you need the absolute value function abs
#include <cstddef> // for NULL
#include "problem.h"
void waldo(Node *¤t) {
if (current == NULL) return;
if (current->left != NULL && current->left->value < current->value) {
Node *tempLeft = current->left->left;
Node *tempRight = current->left->right;
current->left->left = current;
current->left->right = current->right;
Node *tempCurrent = current->left;
current->left = tempLeft;
current->right = tempRight;
current = tempCurrent;
}
waldo(current->left);
waldo(current->right);
}