Question

In: Computer Science

Given a BST and a sum, write pseudo code to determine if the tree has a...

Given a BST and a sum, write pseudo code to determine if the tree has a root- to-leaf path such that adding up all the values along the path equals the given sum. Given the below BST and sum = 49, the array is (8, 4, 10, 1, 0, 3, 9, 15, 16). Return true, as there exist a root-to-leaf path 8− > 10− > 15− > 16 which sum is 49.

Solutions

Expert Solution

Pseudocode

==================

Input:

  • BST root node
  • sum used to determine if the tree has a root- to-leaf path

Output:

Returns true if the tree has a root to leaf path with given sum, else false

function haspathSum (Node root, sum)

    {

        if (node is null) {

            return true if sum = 0

}

        else {

ans = false;

subsum = sum - node.data;

            if (subsum is 0 and node.left is null and node.right is null)

                return true;

            if (node.left is not null)

                ans = (ans) OR (haspathSum(node.left, subsum));

            if (node.right is not null)

                ans = (ans) OR haspathSum(node.right, subsum));

            return ans;

        }

    }


Related Solutions

Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a routine that will mark (use a negative number like -999 for the info field) every node in the tree that currently has only a left son. You can assume the root of the tree has both a right and left son. When finished tell me how many nodes had only a left son as well as how many nodes are in the tree in...
I was trying to implement a simple binary search tree using this given class of bst...
I was trying to implement a simple binary search tree using this given class of bst in c++ public: BST(); ~BST(); void insertKey(int newKey); bool hasKey(int searchKey); std::vector<int> inOrder(); int getHeight(); however; i am still required to use another class for the nodes as a pointer and i need to manage memory leak. in main we should ask for the numbers we need to insert in the binary search tree and also let the user end it with a letter...
In java, Finding the maximum value in a BST (Binary Search Tree). Students need to write...
In java, Finding the maximum value in a BST (Binary Search Tree). Students need to write the code.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
1. Write an algorithm to calculate the Matrix multiplication (or write with pseudo code) 2. Write...
1. Write an algorithm to calculate the Matrix multiplication (or write with pseudo code) 2. Write an algorithm to calculate the recursive Matrix multiplication (or write with pseudo code) 3. Find the time complexity of your pseudo code and analyze the differences
A customer in a grocery store is purchasing three items. Write the pseudo code that will:...
A customer in a grocery store is purchasing three items. Write the pseudo code that will: • Ask the user to enter the name of the first item purchased. Then ask the user to enter the cost of the first item purchased. Make your program user friendly. If the user says the first item purchased is milk, then ask: “What is the cost of milk.” [This should work no matter what item is entered by the user. I might buy...
Write an algorithm in pseudo code to find one element and delete it in a doubly...
Write an algorithm in pseudo code to find one element and delete it in a doubly linked list. Your algorithm will print the original list, request the user to put in an element to be deleted, then print the final list after the deletion is done. If the element doesn’t exist in the list, print "XXX is not in the list" where "XXX" should be the one you received from the user. Use the following as your test cases to...
Write a pseudo code program for a goal-based agent. The goal of the agent is to...
Write a pseudo code program for a goal-based agent. The goal of the agent is to find the exit of a labyrinth. The agent is not omniscient The agent can sense if it is next to a wall (in front, left or right) The agent can turn 90 degrees to the right or left The agent can drive 1unit forward The maze is constructed of paths that are 1 unit across (wide) Show a maze of your choosing and illustrate...
• This assignment calls for generating a Binary Search Tree (BST) and scanning it in a...
• This assignment calls for generating a Binary Search Tree (BST) and scanning it in a preorder and a Breadth First Search (BFS) way. • The program gets a parameter ? from the command line and stores ? randomly generated integers in a dynamic array. To generate a random integer between 1 and Max-Rand (a system constant): ▪ Use srand() to seed the rand() function. Then, call rand() again and again (? times) to generate ? random integers. Next, printout...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT