Skip to content

Commit 4cb8f00

Browse files
authored
Merge pull request #3571 from Shraman-jain/gfg-Q111
Gfg q111
2 parents 36a29d4 + ff865e2 commit 4cb8f00

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

dsa-problems/gfg-problems/easy/0101-0200.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const problems = [
7373
{
7474
"difficulty": "Easy",
7575
"gfgLink": "https://www.geeksforgeeks.org/problems/k-distance-from-root/1",
76-
"solutionLink": "#",
76+
"solutionLink": "/dsa-solutions/gfg-solutions/easy/k-distance-from-root",
7777
"problemName": "K distance from root"
7878
},
7979
{
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
id: k-distance-from-root
3+
title: K Distance From Root
4+
sidebar_label: 0111-K Distance From Root
5+
tags:
6+
- Tree
7+
- Data Structures
8+
description: "A solution to the problem of finding all the nodes which are at a distance k from the root"
9+
---
10+
11+
In this page, we will solve the problem of finding all the nodes which are at a distance k from the root.
12+
13+
## Problem Description
14+
15+
Given a binary tree having n nodes and an integer k. Print all nodes that are at distance k from the root (root is considered at distance 0 from itself). Nodes should be printed from left to right.
16+
### Examples
17+
18+
**Example 1:**
19+
20+
```plaintext
21+
Input:
22+
k = 0
23+
1
24+
/ \
25+
3 2
26+
Output:
27+
1
28+
Explanation:
29+
1 is the only node which is 0 distance from the root 1.
30+
```
31+
32+
**Example 2:**
33+
34+
```plaintext
35+
Input:
36+
k = 3
37+
1
38+
/
39+
2
40+
\
41+
1
42+
/ \
43+
5 3
44+
Output:
45+
5 3
46+
Explanation:
47+
5 and 3 are the nodes which are at distance 3 from the root 3.
48+
Here, returning 3 5 will be incorrect.
49+
```
50+
51+
52+
### Constraints
53+
54+
- $1 \leq$ n $\leq10^4$
55+
- $0 \leq$ k $\leq30$
56+
57+
## Solution
58+
59+
### Intuition and Approach
60+
61+
The problem can be solved by using BFS traversal.This approach combines setting parent references to enable upward traversal and performing a breadth-first search to find nodes at the specified distance.
62+
Adding parent references to each node, we can easily move from a node to its parent, facilitating traversal in all directions (up, left, right).
63+
Starting from the target node, we explore all nodes at increasing distances.
64+
By keeping track of visited nodes, we ensure that each node is processed only once, preventing cycles and redundant work.
65+
When the distance from the target node matches k, we add the node's value to the result list.
66+
67+
### Approach: BFS
68+
1. We will first create a helper function make_parent that recursively traverses the tree, setting a parent attribute for each node. This allows upward traversal.Then we call make_parent with the root node and None as the parent.
69+
2. We then initialize data structures
70+
- ans: A list to store the values of nodes at distance k from the target node.
71+
- seen: A set to track visited nodes and prevent reprocessing.
72+
3. We will create a recursive traversal function trav. This function takes the current node and the distance from the target node as arguments.
73+
- If the current node is None, already visited, or the distance exceeds k, the function returns.
74+
- Add the current node to the seen set to mark it as visited.
75+
- If the current distance matches k, add the node's value to the ans list.
76+
- Then recursively call trav for the parent, left child, and right child, increasing the distance by 1 for each call.
77+
4. Start the traversal from the root node with an initial distance of 0.
78+
5. Return the ans list containing the values of nodes at distance k from the root.
79+
80+
### Code in Python
81+
```python
82+
class Solution:
83+
def KDistance(self,root,k):
84+
'''
85+
:param root: root of given tree.
86+
:param k: distance k from root
87+
:return: list of all nodes that are at distance k from root.
88+
'''
89+
# code here
90+
def make_parent(node,parent):
91+
if not node:
92+
return
93+
node.parent=parent
94+
make_parent(node.left,node)
95+
make_parent(node.right,node)
96+
97+
make_parent(root,None)
98+
ans=[]
99+
seen=set()
100+
101+
def trav(node,dist):
102+
if not node or node in seen or dist>k:
103+
return
104+
seen.add(node)
105+
if dist==k:
106+
ans.append(node.data)
107+
return
108+
trav(node.parent,dist+1)
109+
trav(node.left,dist+1)
110+
trav(node.right,dist+1)
111+
trav(root,0)
112+
return ans
113+
```
114+
115+
### Complexity Analysis
116+
117+
- **Time Complexity:** $O(N)$
118+
- **Space Complexity:** $O(N)$
119+

0 commit comments

Comments
 (0)