1. (30 pts) Given a string representing the root of a binary tree (NOTE: Not a BST, there is no sort order) and an integer, return whether or not there is a root-to-leaf path such that adding up all the values along the path equals the integer. Output a 1 if the result is true, and a 0 if the result is false. You must use a Binary Tree implementation (NO STL) for the input to receive credit. Feel free to include any variables you will need in your functions. Case 1: Input 1: 1 2 3 Output 1: 1 3 Case 2: Input 2: 6 7 8 27 1 3 9 null1 4 null null null 5 Output 2: 0 20 Case 3: Input 3: 5 4 8 11 null 13 4 7 2 null null null 1 Output 3: 1 22 Assumptions: The given strings will contain no more than 50 inputs. The given input starts at the root and builds the tree sequentially from left to right, as shown below "null" represents an empty space in a given level. The integer to be checked is located on a separate line from the binary tree, and thus can be read in another input or as a separate line in a file. You may use cin/cout or input/output files for submission. 2 b) 4 Example Input: 1 2 7 5 4 null 9 Output if integer searched = 7: 1 Output if integer searched = 3: 0 Definition of root-to-leaf: A root-to-leaf path is a single path that goes from the root of the Tree to one of its leaf nodes in a straight line, such that only a single node is traversed on each level. So if there is a tree with 4 levels, a root-to-leaf path contains at most 4 nodes, but could contain less as long as it reaches a leaf node. The final node is required to be a leaf node: stopping early is NOT a solution. Valid answers only occur when one starts at the root and ends at a leaf.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Given a string representing the root of a binary tree (NOTE: Not a BST, there is no sort order) and an integer, return whether or not there is a root-to-leaf path such that adding up all the values along the path equals the integer. Output a 1 if the result is true, and a 0 if the result is false. You must use a Binary Tree implementation (NO STL) for the input to receive credit. Feel free to include any variables you will need in your functions.

 

 

Assumptions: The given strings will contain no more than 50 inputs. The given input starts at the root and builds the tree sequentially from left to right, as shown below "null" represents an empty space in a given level. The integer to be checked is located on a separate line from the binary tree, and thus can be read in another input or as a separate line in a file. You may use cin/cout or input/output files for submission.

 

Definition of root-to-leaf: A root-to-leaf path is a single path that goes from the root of the Tree to one of its leaf nodes in a straight line, such that only a single node is traversed on each level. So if there is a tree with 4 levels, a root-to-leaf path contains at most 4 nodes, but could contain less as long as it reaches a leaf node. The final node is required to be a leaf node: stopping early is NOT a solution. Valid answers only occur when one starts at the root and ends at a leaf.

1. (30 pts) Given a string representing the root of a binary tree (NOTE: Not a BST, there is
no sort order) and an integer, return whether or not there is a root-to-leaf path such that
adding up all the values along the path equals the integer. Output a 1 if the result is true,
and a 0 if the result is false. You must use a Binary Tree implementation (NO STL) for the
input to receive credit. Feel free to include any variables you will need in your functions.
Case 1:
Input 1: 123
Output 1: 1
Case 2:
Input 2: 6 7 8 2713 9 null 1 4 null null null 5
Output 2: 0
20
Case 3:
Input 3: 5 4 8 11 null 13 4 7 2 null null null 1
Output 3: 1
22
Assumptions: The given strings will contain no more than 50 inputs. The given input
starts at the root and builds the tree sequentially from left to right, as shown below "null"
represents an empty space in a given level. The integer to be checked is located on a
separate line from the binary tree, and thus can be read in another input or as a separate
line in a file. You may use cin/cout or input/output files for submission.
2
4
Example Input: 1 2 7 5 4 null 9
Output if integer searched = 7: 1
Output if integer searched = 3: 0
Definition of root-to-leaf: A root-to-leaf path is a single path that goes from the root of
the Tree to one of its leaf nodes in a straight line, such that only a single node is traversed
on each level. So if there is a tree with 4 levels, a root-to-leaf path contains at most 4
nodes, but could contain less as long as it reaches a leaf node. The final node is required
to be a leaf node: stopping early is NOT a solution. Valid answers only occur when one
starts at the root and ends at a leaf.
Transcribed Image Text:1. (30 pts) Given a string representing the root of a binary tree (NOTE: Not a BST, there is no sort order) and an integer, return whether or not there is a root-to-leaf path such that adding up all the values along the path equals the integer. Output a 1 if the result is true, and a 0 if the result is false. You must use a Binary Tree implementation (NO STL) for the input to receive credit. Feel free to include any variables you will need in your functions. Case 1: Input 1: 123 Output 1: 1 Case 2: Input 2: 6 7 8 2713 9 null 1 4 null null null 5 Output 2: 0 20 Case 3: Input 3: 5 4 8 11 null 13 4 7 2 null null null 1 Output 3: 1 22 Assumptions: The given strings will contain no more than 50 inputs. The given input starts at the root and builds the tree sequentially from left to right, as shown below "null" represents an empty space in a given level. The integer to be checked is located on a separate line from the binary tree, and thus can be read in another input or as a separate line in a file. You may use cin/cout or input/output files for submission. 2 4 Example Input: 1 2 7 5 4 null 9 Output if integer searched = 7: 1 Output if integer searched = 3: 0 Definition of root-to-leaf: A root-to-leaf path is a single path that goes from the root of the Tree to one of its leaf nodes in a straight line, such that only a single node is traversed on each level. So if there is a tree with 4 levels, a root-to-leaf path contains at most 4 nodes, but could contain less as long as it reaches a leaf node. The final node is required to be a leaf node: stopping early is NOT a solution. Valid answers only occur when one starts at the root and ends at a leaf.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY