|
| 1 | +#include <iostream> |
| 2 | +#include <algorithm> |
| 3 | +#include <climits> |
| 4 | +#include <vector> |
| 5 | +#include <stack> |
| 6 | +#include <set> |
| 7 | +#include <queue> |
| 8 | +#include <map> |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +/* |
| 12 | +----------------------------------------------------- |
| 13 | +🌳 All Nodes Distance K in Binary Tree |
| 14 | +----------------------------------------------------- |
| 15 | +📌 Description: |
| 16 | +Given a binary tree, a target node, and an integer K, |
| 17 | +find all nodes in the tree that are exactly K distance |
| 18 | +away from the target node. |
| 19 | +
|
| 20 | +Approach: |
| 21 | +1. First, traverse the tree to mark each node's parent. |
| 22 | +2. Then, perform a BFS starting from the target node, |
| 23 | + moving both to children and parent, keeping track of visited nodes. |
| 24 | +3. Collect all nodes at distance K. |
| 25 | +
|
| 26 | +📊 Complexity Analysis: |
| 27 | +- Time Complexity: O(N), where N is the number of nodes in the tree. |
| 28 | + We visit each node at most twice (once for marking parents, once for BFS). |
| 29 | +- Space Complexity: O(N), for parent mapping, visited map, and BFS queue. |
| 30 | +
|
| 31 | +✅ Applications: |
| 32 | +- Tree-based graph problems |
| 33 | +- Distance-related queries in trees |
| 34 | +- Competitive programming / coding interviews |
| 35 | +----------------------------------------------------- |
| 36 | +*/ |
| 37 | + |
| 38 | +// Definition for a binary tree node |
| 39 | +struct TreeNode { |
| 40 | + int val; |
| 41 | + TreeNode *left; |
| 42 | + TreeNode *right; |
| 43 | + TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| 44 | +}; |
| 45 | + |
| 46 | +// Solution class |
| 47 | +class Solution { |
| 48 | + // Helper function to mark parents of each node |
| 49 | + void markParents(TreeNode* root, unordered_map<TreeNode*, TreeNode*>& parent) { |
| 50 | + queue<TreeNode*> q; |
| 51 | + q.push(root); |
| 52 | + while (!q.empty()) { |
| 53 | + TreeNode* curr = q.front(); |
| 54 | + q.pop(); |
| 55 | + if (curr->left) { |
| 56 | + parent[curr->left] = curr; |
| 57 | + q.push(curr->left); |
| 58 | + } |
| 59 | + if (curr->right) { |
| 60 | + parent[curr->right] = curr; |
| 61 | + q.push(curr->right); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | +public: |
| 67 | + vector<int> distanceK(TreeNode* root, TreeNode* target, int K) { |
| 68 | + unordered_map<TreeNode*, TreeNode*> parent; |
| 69 | + markParents(root, parent); |
| 70 | + |
| 71 | + unordered_map<TreeNode*, bool> visited; |
| 72 | + queue<TreeNode*> q; |
| 73 | + q.push(target); |
| 74 | + visited[target] = true; |
| 75 | + |
| 76 | + int currLevel = 0; |
| 77 | + while (!q.empty()) { |
| 78 | + int size = q.size(); |
| 79 | + if (currLevel++ == K) break; |
| 80 | + for (int i = 0; i < size; i++) { |
| 81 | + TreeNode* curr = q.front(); |
| 82 | + q.pop(); |
| 83 | + |
| 84 | + if (curr->left && !visited[curr->left]) { |
| 85 | + q.push(curr->left); |
| 86 | + visited[curr->left] = true; |
| 87 | + } |
| 88 | + if (curr->right && !visited[curr->right]) { |
| 89 | + q.push(curr->right); |
| 90 | + visited[curr->right] = true; |
| 91 | + } |
| 92 | + if (parent[curr] && !visited[parent[curr]]) { |
| 93 | + q.push(parent[curr]); |
| 94 | + visited[parent[curr]] = true; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + vector<int> res; |
| 100 | + while (!q.empty()) { |
| 101 | + TreeNode* curr = q.front(); |
| 102 | + q.pop(); |
| 103 | + res.push_back(curr->val); |
| 104 | + } |
| 105 | + return res; |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +// Utility function to create a sample tree |
| 110 | +TreeNode* createSampleTree() { |
| 111 | + TreeNode* root = new TreeNode(3); |
| 112 | + root->left = new TreeNode(5); |
| 113 | + root->right = new TreeNode(1); |
| 114 | + root->left->left = new TreeNode(6); |
| 115 | + root->left->right = new TreeNode(2); |
| 116 | + root->right->left = new TreeNode(0); |
| 117 | + root->right->right = new TreeNode(8); |
| 118 | + root->left->right->left = new TreeNode(7); |
| 119 | + root->left->right->right = new TreeNode(4); |
| 120 | + return root; |
| 121 | +} |
| 122 | + |
| 123 | +// Driver function |
| 124 | +int main() { |
| 125 | + cout << "All Nodes Distance K in Binary Tree implementation." << endl; |
| 126 | + |
| 127 | + TreeNode* root = createSampleTree(); |
| 128 | + TreeNode* target = root->left; // Node with value 5 |
| 129 | + int K = 2; |
| 130 | + |
| 131 | + Solution sol; |
| 132 | + vector<int> result = sol.distanceK(root, target, K); |
| 133 | + |
| 134 | + cout << "Nodes at distance " << K << " from target (" << target->val << "): "; |
| 135 | + for (int val : result) { |
| 136 | + cout << val << " "; |
| 137 | + } |
| 138 | + cout << endl; |
| 139 | + |
| 140 | + return 0; |
| 141 | +} |
0 commit comments