Input: root =[1,2,3,4,5,null,null], X =2Output: [4,5]Explanation:
The nodes at a distance of 2 from the root node 1 are 4 and 5.- Distance 2 from root 1-> Node 4- Distance 2 from root 1-> Node 5
Input: root =[1,2,3,null,4,5,null], X =1Output: [2,3]Explanation:
The nodes at a distance of 1 from the root node 1 are 2 and 3.- Distance 1 from root 1-> Node 2- Distance 1 from root 1-> Node 3
To find all the nodes at distance X from the root, we can use Depth First Search (DFS) or Breadth First Search (BFS). The idea is to traverse the tree and keep track of the current depth. When the depth equals X, we collect the node’s value.