Find distance between two given keys of a Binary Tree | GeeksforGeeks
Find the distance between 2 nodes in Binary Tree ~ KodeKnight
Find the distance between two keys in a binary tree, no parent pointers are given. Distance between two nodes is the minimum number of edges to be traversed to reach one node from other.
Example
Dist(-4,3) = 2
Dist(n1, n2) = Dist(root, n1) + Dist(root, n2) - 2*Dist(root, lca)
'n1' and 'n2' are the two given keys
'root' is root of given Binary Tree.
'lca' is lowest common ancestor of n1 and n2
Dist(n1, n2) is the distance between n1 and n2.
http://hehejun.blogspot.com/2015/01/algorithmget-distance-between-two-nodes.html
两个Node的距离取决于从node1到node2经过的edge数,如果考虑用类似LCA的解法的话,如果两个node的LCA是其中一个node,因为我们的算法不会再往下递归了,所以没法得到两者的距离。所以这里我们先计算LCA,之后算出node1和node2和LCA的depth,depth1 + depth2 - 2 * depthLCA就是最终的结果。找LCA的时间复杂度是O(n),因为是binary tree,找depth的时间复杂度也是O(n),所以总的时间复杂度是O(n)
public int getDistanace(TreeNode root, TreeNode node1, TreeNode node2) {
if (root == null || node1 == null || node2 == null)
return -1;
TreeNode ancestor = LCA(root, node1, node2); // error handling, if no lca
int depth1 = getDepth(root, ancestor);
int depth2 = getDepth(root, node1);
int depth3 = getDepth(root, node2);
return depth2 + depth3 - 2 * depth1;
}
private TreeNode LCA(TreeNode curr, TreeNode node1, TreeNode node2) {
if (curr == null)
return null;
if (curr == node1 || curr == node2)
return curr;
TreeNode left = LCA(curr.left, node1, node2);
TreeNode right = LCA(curr.right, node1, node2);
if(left != null && right != null)
return curr;
return left == null? right: left;
}
private int getDepth(TreeNode curr, TreeNode target) {
if (curr == null)
return -1;
if (curr == target)
return 0;
int left = getDepth(curr.left, target);
int right = getDepth(curr.right, target);
if (left == -1 && right == -1)
return -1;
return left == -1? right + 1: left + 1;
}
Read full article from Find distance between two given keys of a Binary Tree | GeeksforGeeks
Find the distance between 2 nodes in Binary Tree ~ KodeKnight
Find the distance between two keys in a binary tree, no parent pointers are given. Distance between two nodes is the minimum number of edges to be traversed to reach one node from other.
Example
Dist(-4,3) = 2
Dist(n1, n2) = Dist(root, n1) + Dist(root, n2) - 2*Dist(root, lca)
'n1' and 'n2' are the two given keys
'root' is root of given Binary Tree.
'lca' is lowest common ancestor of n1 and n2
Dist(n1, n2) is the distance between n1 and n2.
http://hehejun.blogspot.com/2015/01/algorithmget-distance-between-two-nodes.html
两个Node的距离取决于从node1到node2经过的edge数,如果考虑用类似LCA的解法的话,如果两个node的LCA是其中一个node,因为我们的算法不会再往下递归了,所以没法得到两者的距离。所以这里我们先计算LCA,之后算出node1和node2和LCA的depth,depth1 + depth2 - 2 * depthLCA就是最终的结果。找LCA的时间复杂度是O(n),因为是binary tree,找depth的时间复杂度也是O(n),所以总的时间复杂度是O(n)
public int getDistanace(TreeNode root, TreeNode node1, TreeNode node2) {
if (root == null || node1 == null || node2 == null)
return -1;
TreeNode ancestor = LCA(root, node1, node2); // error handling, if no lca
int depth1 = getDepth(root, ancestor);
int depth2 = getDepth(root, node1);
int depth3 = getDepth(root, node2);
return depth2 + depth3 - 2 * depth1;
}
private TreeNode LCA(TreeNode curr, TreeNode node1, TreeNode node2) {
if (curr == null)
return null;
if (curr == node1 || curr == node2)
return curr;
TreeNode left = LCA(curr.left, node1, node2);
TreeNode right = LCA(curr.right, node1, node2);
if(left != null && right != null)
return curr;
return left == null? right: left;
}
private int getDepth(TreeNode curr, TreeNode target) {
if (curr == null)
return -1;
if (curr == target)
return 0;
int left = getDepth(curr.left, target);
int right = getDepth(curr.right, target);
if (left == -1 && right == -1)
return -1;
return left == -1? right + 1: left + 1;
}
// Returns level of key k if it is present in tree, otherwise returns -1
int
findLevel(Node root,
int
k,
int
level)
{
// Base Case
if
(root ==
null
)
return
-
1
;
// If key is present at root, or in left subtree or right subtree,
// return true;
if
(root.key == k)
return
level;
int
l = findLevel(root.left, k, level+
1
);
return
(l != -
1
)? l : findLevel(root.right, k, level+
1
);
}
// This function returns pointer to LCA of two given values n1 and n2.
// It also sets d1, d2 and dist if one key is not ancestor of other
// Note that we set the value in findDistUtil for d1,d2 and dist
// d1 -. To store distance of n1 from root
// d2 -. To store distance of n2 from root
// lvl -. Level (or distance from root) of current node
// dist -. To store distance between n1 and n2
Node findDistUtil(Node root,
int
n1,
int
n2, Integer d1, Integer d2,
Integer dist,
int
lvl)
{
// Base case
if
(root ==
null
)
return
null
;
// If either n1 or n2 matches with root's key, report
// the presence by returning root (Note that if a key is
// ancestor of other, then the ancestor key becomes LCA
if
(root.key == n1)
{
d1 = lvl;
return
root;
}
if
(root.key == n2)
{
d2 = lvl;
return
root;
}
// Look for n1 and n2 in left and right subtrees
Node left_lca = findDistUtil(root.left, n1, n2, d1, d2, dist, lvl+
1
);
Node right_lca = findDistUtil(root.right, n1, n2, d1, d2, dist, lvl+
1
);
// If both of the above calls return Non-null, then one key
// is present in once subtree and other is present in other,
// So this node is the LCA
if
(left_lca!=
null
&& right_lca!=
null
)
{
dist = d1 + d2 -
2
*lvl;
return
root;
}
// Otherwise check if left subtree or right subtree is LCA
return
(left_lca !=
null
)? left_lca: right_lca;
}
// The main function that returns distance between n1 and n2
// This function returns -1 if either n1 or n2 is not present in
// Binary Tree.
int
findDistance(Node root,
int
n1,
int
n2)
{
// Initialize d1 (distance of n1 from root), d2 (distance of n2
// from root) and dist(distance between n1 and n2)
Integer d1 = -
1
, d2 = -
1
, dist;
Node lca = findDistUtil(root, n1, n2, d1, d2, dist,
1
);
// If both n1 and n2 were present in Binary Tree, return dist
if
(d1 != -
1
&& d2 != -
1
)
return
dist;
// If n1 is ancestor of n2, consider n1 as root and find level
// of n2 in subtree rooted with n1
if
(d1 != -
1
)
{
dist = findLevel(lca, n2,
0
);
return
dist;
}
// If n2 is ancestor of n1, consider n2 as root and find level
// of n1 in subtree rooted with n2
if
(d2 != -
1
)
{
dist = findLevel(lca, n1,
0
);
return
dist;
}
return
-
1
;
}
The distance between two nodes can be obtained in terms of lowest common ancestor.
Dist(n1, n2) = Dist(root, n1) + Dist(root, n2) - 2*Dist(root, lca)
// Returns level of key k if it is present in tree, otherwise returns -1
int
findLevel(Node *root,
int
k,
int
level)
{
// Base Case
if
(root == NULL)
return
-1;
// If key is present at root, or in left subtree or right subtree,
// return true;
if
(root->key == k)
return
level;
int
l = findLevel(root->left, k, level+1);
return
(l != -1)? l : findLevel(root->right, k, level+1);
}
// This function returns pointer to LCA of two given values n1 and n2.
// It also sets d1, d2 and dist if one key is not ancestor of other
// d1 --> To store distance of n1 from root
// d2 --> To store distance of n2 from root
// lvl --> Level (or distance from root) of current node
// dist --> To store distance between n1 and n2
Node *findDistUtil(Node* root,
int
n1,
int
n2,
int
&d1,
int
&d2,
int
&dist,
int
lvl)
{
// Base case
if
(root == NULL)
return
NULL;
// If either n1 or n2 matches with root's key, report
// the presence by returning root (Note that if a key is
// ancestor of other, then the ancestor key becomes LCA
if
(root->key == n1)
{
d1 = lvl;
return
root;
}
if
(root->key == n2)
{
d2 = lvl;
return
root;
}
// Look for n1 and n2 in left and right subtrees
Node *left_lca = findDistUtil(root->left, n1, n2, d1, d2, dist, lvl+1);
Node *right_lca = findDistUtil(root->right, n1, n2, d1, d2, dist, lvl+1);
// If both of the above calls return Non-NULL, then one key
// is present in once subtree and other is present in other,
// So this node is the LCA
if
(left_lca && right_lca)
{
dist = d1 + d2 - 2*lvl;
return
root;
}
// Otherwise check if left subtree or right subtree is LCA
return
(left_lca != NULL)? left_lca: right_lca;
}
// The main function that returns distance between n1 and n2
// This function returns -1 if either n1 or n2 is not present in
// Binary Tree.
int
findDistance(Node *root,
int
n1,
int
n2)
{
// Initialize d1 (distance of n1 from root), d2 (distance of n2
// from root) and dist(distance between n1 and n2)
int
d1 = -1, d2 = -1, dist;
Node *lca = findDistUtil(root, n1, n2, d1, d2, dist, 1);
// If both n1 and n2 were present in Binary Tree, return dist
if
(d1 != -1 && d2 != -1)
return
dist;
// If n1 is ancestor of n2, consider n1 as root and find level
// of n2 in subtree rooted with n1
if
(d1 != -1)
{
dist = findLevel(lca, n2, 0);
return
dist;
}
// If n2 is ancestor of n1, consider n2 as root and find level
// of n1 in subtree rooted with n2
if
(d2 != -1)
{
dist = findLevel(lca, n1, 0);
return
dist;
}
return
-1;
}
Read full article from Find distance between two given keys of a Binary Tree | GeeksforGeeks