Problem 3 (30 points) Complete the code of a recursive function findPath below t
ID: 3702371 • Letter: P
Question
Problem 3 (30 points) Complete the code of a recursive function findPath below that takes a pointer to the root of a binary tree and returns a vector of keys on a path (any path) from the root to the node with a given key value. This function has the following arguments: root is a pointer to the root of a tree path is a reference to a vector of keys of nodes along the path target is the key of the node to which a path needs to be found // find a path from root node to a given node and store the found path in a vector path // return true if a path exists otherwise false bool findPath(Node *root, std:vector int> &path;, int target)Explanation / Answer
struct Node { int key; Node *left, *right; }; bool findPath(Node *root, std::vector &path, int target) { if(root == NULL) { return false; } else { path.push_back(root->key); if(root->key == target) { return true; } else if(target key) { return findPath(root->left, path, target); } else { return findPath(root->right, path, target); } } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.