/*LCA of Binary TreeSend FeedbackGiven a binary tree and data of two nodes, find 'LCA' (Lowest Common Ancestor) of the given two nodes in th binary tree.LCALCA of two nodes A and B is the lowest or deepest node which has both A and B as its descendants. Example: In this example, the green coloured node is the ICA to A and B Alt TextNote:It is defined that

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
/*LCA of Binary TreeSend FeedbackGiven a binary tree and data of two
nodes, find 'LCA' (Lowest Common Ancestor) of the given two nodes in the
binary tree.LCALCA of two nodes A and B is the lowest or deepest node
which has both A and B as its descendants. Example: In this example, the
green coloured node is the LCA to A and B.Alt TextNote:It is defined that
each node is a descendant to itself, so, if there are two nodes X and Y and
X has a direct connection from Y, then Y is the lowest common
ancestor. Example:Alt TextNote: 1. If out of 2 nodes only one node is
present, return that node. 2. If both are not present, return -1.Input
format: The first line of input contains data of the nodes of the tree in level
order form. The data of the nodes of the tree is separated by space. If any
node does not have left or right child, take -1 in its place. Since -1 is used
as an indication whether the left or right nodes exist, therefore, it will not be
a part of the data of any node. The following line of input contains two
integers, denoting the value of data of two nodes of given binary
tree.Output Format:The first and only line of output contains the data
associated with the lowest common ancestor node.Constraints: Time
Limit: 1 secondSample Input 1:5 10 6 2 3 -1 -1 -1 -1 -1 9 -1 -12 10Sample
Output 1:10Sample Input 2:5 10 6 2 3-1 -1 -1 -1 -1 9 -1 -12 6Sample Output
2:5Sample Input 3:5 10 6 2 3 -1 -1 -1 -1 -1 9 -1 -112 78Sample Output 3:-1*/
Transcribed Image Text:/*LCA of Binary TreeSend FeedbackGiven a binary tree and data of two nodes, find 'LCA' (Lowest Common Ancestor) of the given two nodes in the binary tree.LCALCA of two nodes A and B is the lowest or deepest node which has both A and B as its descendants. Example: In this example, the green coloured node is the LCA to A and B.Alt TextNote:It is defined that each node is a descendant to itself, so, if there are two nodes X and Y and X has a direct connection from Y, then Y is the lowest common ancestor. Example:Alt TextNote: 1. If out of 2 nodes only one node is present, return that node. 2. If both are not present, return -1.Input format: The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. If any node does not have left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node. The following line of input contains two integers, denoting the value of data of two nodes of given binary tree.Output Format:The first and only line of output contains the data associated with the lowest common ancestor node.Constraints: Time Limit: 1 secondSample Input 1:5 10 6 2 3 -1 -1 -1 -1 -1 9 -1 -12 10Sample Output 1:10Sample Input 2:5 10 6 2 3-1 -1 -1 -1 -1 9 -1 -12 6Sample Output 2:5Sample Input 3:5 10 6 2 3 -1 -1 -1 -1 -1 9 -1 -112 78Sample Output 3:-1*/
Expert Solution
steps

Step by step

Solved in 3 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