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.
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.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images