Question: C + + 3 Path Sum Given a binary tree and an integer targetSum, return true if the tree has a root - to -

C++
3Path Sum
Given a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.
Example 1:
targetSum =22
Output: true
Explanation: The root-to-leaf path with the target sum is shown.
Example 2: targetSum =5
Output: false Explanation: There is no root-to-leaf path with sum =5.
Input
1. The first line contains a single integer n indicates the length of array inorder (inorder and postorder have the same length).
2. The second line contains n integers in1, in2,..., inn separated by spaces, which indicate the the inorder traversal of the binary tree.
3. The third line containsnintegers post1, post2,..., postn separated by spaces, which indicate the the postorder traversal of the binary tree.
Output Print true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.
Sample Input
9
711245138151
721141311585
Sample Output
true

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!