给定一个二叉树(具有根结点 root), 一个目标结点 target ,和一个整数值 K 。
返回到目标结点 target 距离为 K 的所有结点的值的列表。 答案可以以任何顺序返回。
示例 1:
输入:root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2
输出:[7,4,1]
解释:
所求结点为与目标结点(值为 5)距离为 2 的结点,
值分别为 7,4,以及 1
注意,输入的 "root" 和 "target" 实际上是树上的结点。
上面的输入仅仅是对这些对象进行了序列化描述。
提示:
给定的树是非空的,且最多有 K 个结点。
树上的每个结点都具有唯一的值 0 <= node.val <= 500 。
目标结点 target 是树上的结点。
0 <= K <= 1000.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/all-nodes-distance-k-in-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
方法1:以target结点出发的深度优先遍历
解题思路:
考虑2种情况
1:target子结点怎么找。
2:target祖先结点该怎么找。
子节点很简单下一层 距离 -1就行。
祖先结点要考虑,taget结点 到 祖先 结点的距离,然后先把k减去这个距离-1下到另一个分支去找。
代码实现:
class Solution {
public:
vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
if ( K == 0) return vector<int>{target->val};
vector<int> res;
distanceChild(target,res,K);
getParentDistance(root,res,target,K);
return res;
}
int getParentDistance(TreeNode* root,vector<int>& res,TreeNode* target, int K)
{
if (!root) return 0;
if (root == target) return 1; //找到tatget结点 返回给父结点距离为1
int left = getParentDistance(root->left,res,target,K);
int right = getParentDistance(root->right,res,target,K);
if (left > 0) //target结点在左分支
{
if (left == K)
{
res.push_back(root->val);
}
else if (left < K)
{
//去右分支找找,先减去,target结点到当前结点的距离
distanceChild(root->right,res,K - left - 1);
}
return ++left; //返回给祖先结点时距离加1
}
if (right > 0)
{
if (right == K)
{
res.push_back(root->val);
}
else if (right < K)
{
distanceChild(root->left,res,K - right - 1);
}
return ++right;
}
return 0;
}
void distanceChild(TreeNode* root,vector<int>& res,int K)
{
if (!root || K < 0) return;
if (K == 0)
{
res.push_back(root->val);
return;
}
distanceChild(root->left,res,K - 1);
distanceChild(root->right,res,K - 1);
}
};
复杂度分析:
时间复杂度:(O)N 遍历一遍每一个结点,target用了2次不计
空间复杂度:(O)N
方法2:坐标点回溯统计法
解题思路:
先遍历树,给每个结点打上一个坐标值。左结点 N * 2 + 1,右结点 N * 2 + 2
这颗树的所以结点坐标值为:
3 : 0
5 : 坐标[3] * 2 +1 = 1
1 : 坐标[3] * 2 +2 = 2
6:坐标[5] * 2 +1 = 3
2:坐标[5] * 2 +2 = 4
0:坐标[1] * 2 +1 = 5
8:坐标[1] * 2 +2 = 6
7:坐标[2] * 2 +1 = 9
4:坐标[2] * 2 +2 = 10
现在我们来求坐标点之前的距离:
4 到 1 的距离 为 用眼睛可以看出来为4
公式怎么做到呢? 这时候看他们俩的坐标值
4 为 10 ,1 为 2
这时候就要用的回溯了,取一个相反的公式 (较大坐标值 - 1)/ 2
距离 s = 0
(10 - 1 )/ 2 = 4 s ++
(4 - 1) / 2 = 1 s++
这时候 2 > 1 了
(2 - 1) / 2 = 0 s++
这时候 1 > 0 了
(1 - 1)/2 = 0 s++
这时候相等了
得到结果为4
class Solution {
public:
vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
unordered_map<TreeNode *,int> node_DT; //存放结点坐标的哈希表
distance_allNode(root,node_DT,0); //获取所以结点坐标
int targetDT = node_DT[target]; //target坐标值
vector<int> ret;
for (auto& it : node_DT)
{
if (getDisTanceByDT(it.second,targetDT) == K) //遍历回溯距离值
{
ret.push_back(it.first->val);
}
}
return ret;
}
void distance_allNode(TreeNode* root,unordered_map<TreeNode *,int>& node_DT,int DT)
{
if (root == NULL)
{
return;
}
node_DT[root] = DT;
distance_allNode(root->left,node_DT, DT * 2 + 1);
distance_allNode(root->right,node_DT, DT * 2 + 2);
}
int getDisTanceByDT(int compaDT,int targetDT)
{
printf("%d %d ",compaDT,targetDT);
int res = 0;
while (compaDT != targetDT)
{
if (compaDT > targetDT)
{
compaDT = (compaDT - 1) / 2; //回溯距离值公式
}
else
{
targetDT = (targetDT - 1) / 2;
}
++res;
}
printf("%d \n",res);
return res;
}
};
复杂度分析:
时间复杂度:(O)N + (O)logN 遍历一遍放坐标,遍历一遍比距离
空间复杂度:(O)N哈希表存放坐标值