Similar Problems

Similar Problems not available

Nodes are on same path in a tree or Not - Leetcode Solution

LeetCode:  Nodes are on same path in a tree or Not Leetcode Solution

Difficulty: Unknown

Topics: graphs  

Problem Statement

Given a tree and two nodes of tree and the task to check whether these two nodes are on the same path from root to the bottom or not.

Example Input

Node1 = 1

Node2 = 5

Expected Output

Yes

Approach

The idea is to apply Depth First Search on every node. And traverse path from root to every leaf. If path contains the both node then return “Yes” otherwise return “No”.

Nodes are on same path in a tree or Not Solution Code

1