C++ 中二叉树中所有节点的距离为 K
假设我们有一棵二叉树、一个目标节点和一个值 K。我们必须找到与目标节点距离为 K 的所有节点的值列表。
因此,如果输入为 root = [3,5,1,6,2,0,8,null,null,7,4]、target = 5、K = 2,则输出将为 [7,4,1],这是因为距离目标节点 2 的节点的值为 7、4 和 1。
为了解决这个问题,我们将遵循以下步骤−
定义一个函数 dfs(),它将获取 node,pa 用 NULL 初始化它,
如果 node 为 null,则 −
return
parent[node] := pa
dfs(left of node, node)
dfs(right of node, node)
From the main method do the following −
Define an array ans
dfs(root)
Define one queue q for (node, value) pair
insert { target, 0 } into q
Define one set called visited
insert target into visited
while (not q is empty), do −
Define one pair p := first element of q
delete element from q
level := second element of temp
node = first element of temp.
if level is same as k, then −
insert value of node at the end of ans
if left of node is not null and level + 1 <= k and left of node is not visited, then
insert {left of node, level + 1 }) into q
insert left of node into visited set
if right of node is not null and level + 1 <= k and right of node is not visited, then
insert {right of node, level + 1 }) into q
insert right of node into visited set
if parent[node] is not NULL and level + 1 <= k and parent[node] is not visited, then −
insert { parent[node], level + 1 } into q
insert parent[node] into visited
return ans
示例
让我们看下面的实现,以便更好地理解 −
#include <bits/stdc++.h> using namespace std; void print_vector(vector<int> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class TreeNode{ public: int val; TreeNode *left, *right; TreeNode(int data){ val = data; left = NULL; right = NULL; } }; void insert(TreeNode **root, int val){ queue<TreeNode*> q; q.push(*root); while(q.size()){ TreeNode *temp = q.front(); q.pop(); if(!temp->left){ if(val != NULL) temp->left = new TreeNode(val); else temp->left = new TreeNode(0); return; }else{ q.push(temp->left); } if(!temp->right){ if(val != NULL) temp->right = new TreeNode(val); else temp->right = new TreeNode(0); return; }else{ q.push(temp->right); } } } TreeNode *make_tree(vector<int> v){ TreeNode *root = new TreeNode(v[0]); for(int i = 1; i<v.size(); i++){ insert(&root, v[i]); } return root; } class Solution { public: map <TreeNode*, TreeNode*> parent; void dfs(TreeNode* node, TreeNode* pa = NULL){ if (!node) return; parent[node] = pa; dfs(node->left, node); dfs(node->right, node); } vector<int> distanceK(TreeNode* root, TreeNode* target, int k) { vector<int> ans; parent.clear(); dfs(root); queue<pair<TreeNode*, int> > q; q.push({ target, 0 }); set<TreeNode*> visited; visited.insert(target); while (!q.empty()) { pair<TreeNode*, int> temp = q.front(); q.pop(); int level = temp.second; TreeNode* node = temp.first; if (level == k) { ans.push_back(node->val); } if ((node->left && node->left->val != 0) && level + 1 <= k && !visited.count(node->left)) { q.push({ node->left, level + 1 }); visited.insert(node->left); } if ((node->right && node->right->val != 0) && level + 1 <= k && !visited.count(node->right)){ q.push({ node->right, level + 1 }); visited.insert(node->right); } if (parent[node] != NULL && level + 1 <= k && !visited.count(parent[node])) { q.push({ parent[node], level + 1 }); visited.insert(parent[node]); } } return ans; } }; main(){ Solution ob; vector<int> v = {3,5,1,6,2,0,8,NULL,NULL,7,4}; TreeNode *root = make_tree(v); TreeNode *target = root->left; print_vector(ob.distanceK(root, target, 2)); }
输入
{3,5,1,6,2,0,8,NULL,NULL,7,4}
输出
[7, 4, 1, ]