03. K distance from root
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
The problem can be found at the following link: Question Link
If root is null, return.
If k equals 0, push the data of the current root node into the ans vector and return.
Recursively call traverse function for the left child of root with k-1.
Recursively call traverse function for the right child of root with k-1.
Clear the ans vector.
Call traverse function with the root node and distance k.
Return the ans vector containing nodes at distance k from the root.
Time Complexity: O(N)
, where N
is the number of nodes in the tree.
Auxiliary Space Complexity: O(H)
, where H
is the height of the tree.
class Solution
{
public:
// function should print the nodes at k distance from root
vector<int> ans;
void traverse(struct Node *root, int k)
{
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.