All Nodes Distance K in Binary Tree 二叉树中所有距离为 K 的结点

本文探讨了在二叉树中寻找与特定目标节点距离为K的所有节点值的有效算法。通过一次深度优先搜索(DFS),结合哈希映射记录各节点至目标节点的距离,巧妙修正了目标节点子树内节点的距离计算,最终返回距离为K的节点值列表。

给定一个二叉树(具有根结点 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" 实际上是树上的结点。上面的输入仅仅是对这些对象进行了序列化描述。

提示:

  1. 给定的树是非空的,且最多有 K 个结点。
  2. 树上的每个结点都具有唯一的值 0 <= node.val <= 500 。
  3. 目标结点 target 是树上的结点。
  4. 0 <= K <= 1000.

思路:这道题博猪一开始的思路居然是展开成双向链表。。。(愚蠢至极),后来看到了discuss的通过记录已找到的子节点来one pass的方法就可以找到距离为k的节点,实在是巧妙。

思路的核心思想是如果当前节点root距离目标节点的距离为len,那么当前节点的子节点root->left或者root->right距离目标节点的距离就是len+1,除了当前子节点就是目标节点的子树里面的情况(如上图所示,目标节点是5,那么以root节点(3)为起点,它的子节点都比root节点离5更远,除了5,6,2,7,4这5个已经在目标节点的子树或者更靠近目标节点的情况,其他节点都满足这个性质),那么我们要怎么修正对于5,6,2,7,4这5个节点的距离呢?我们就需要一个函数,首先找到以目标节点为叶节点向上追溯到根节点的距离目标节点的值,简单来说就是先通过一个函数把目标节点至根节点的距离值找出来,存入hashmap中,如下所示:

节点5:距离0  (目标节点的距离设为0)

节点2,7,4,6:距离-1  (距离-1表示这些节点是目标节点下面的点或者不在目标节点子树上)

节点3:距离1  (目标节点的父节点的距离逐个加1)

我们把计算出的值存入hashmap中,在dfs过程中如果发现当前节点在hashmap中,就修正对应节点的length,然后看是否和目标K的距离相等即可。

参考代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
int find(unordered_map<TreeNode*, int> &map, TreeNode* root, TreeNode* target) {
	if (root == nullptr) return -1;
	if (root == target) {
		map[root] = 0;
		return 0;
	}
	int left = find(map, root->left, target);
	if (left >= 0) {
		map[root] = (left + 1);
		return left + 1;
	}
	int right = find(map, root->right, target);
	if (right >= 0) {
		map[root] = (right + 1);
		return right + 1;
	}
	return -1;
}
void distanceKCore(vector<int> &res, unordered_map<TreeNode*, int> &map, TreeNode* root, int K,int length) {
	if (root == nullptr) return;
	if (map.find(root) != map.end()) length = map[root];
	if (length == K) res.push_back(root->val);
	distanceKCore(res, map, root->left, K, length + 1);
	distanceKCore(res, map, root->right, K, length + 1);
}
vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
	vector<int> res;
	unordered_map<TreeNode*, int> map;
	find(map, root, target);
	distanceKCore(res, map, root, K,map[root]);
	return res;
}
};

 

 

As an AI language model, I cannot provide you with the exact solution you need without further details. However, here is a general approach to find the longest distance in a binary tree using Python: 1. Define a class for a binary tree node with attributes for the node value, left child, and right child. 2. Define a function that recursively traverses the binary tree and returns the height of the tree starting from a given node. 3. Define a function that recursively traverses the binary tree and returns the longest distance between two nodes in the tree. 4. In the function defined in step 3, for each node, calculate the longest distance that passes through that node and update the maximum distance seen so far. 5. Call the function defined in step 3 starting from the root node of the binary tree. Here is a sample implementation of the above approach: ``` class Node: def __init__(self, value): self.value = value self.left = None self.right = None def height(root): if root is None: return 0 return 1 + max(height(root.left), height(root.right)) def longest_distance(root): if root is None: return 0 left_height = height(root.left) right_height = height(root.right) left_distance = longest_distance(root.left) right_distance = longest_distance(root.right) return max(left_height + right_height, max(left_distance, right_distance)) root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) print(longest_distance(root)) ``` This code will output the longest distance between any two nodes in the binary tree.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值