Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given a binary tree and an integer targetSum, return true if the tree has a root - to - leaf path such that adding up

 

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.

Example2 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

7 11 2 4 5 13 8 15 1

7 2 11 4 13 1 15 8 5


Sample Output

true


Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Precalculus

Authors: Michael Sullivan

9th edition

321716835, 321716833, 978-0321716835

More Books

Students also viewed these Programming questions