问题
Given a binary tree: Binary tree of height 3
I want to find the horizontal distance between two nodes at the same level also counting the nodes that are not there in between while not counting the nodes themselves, Say in
f
/ \
g h
/ \ / \
a d
in between the nodes a and d there is a horizontal distance of 2.
Edit:
Please see distance between a to d is calculated on the same level not including the parent or child nodes of either a or d but only the missing nodes at the same level. so distance between a to d would be a>(x>y)>d where in x and y are the missing child nodes of node g and h respectively. Hence not counting the target nodes a and d there'll be a horizontal distance of 2
回答1:
Think of it this way:
a
/ \
b c
/ \ / \
e f g h
Now, you want to determine horizontal distance between nodes at the same level. Eg: f and g. Here is a step by step approach:
- Perform a level order traversal of the binary tree and store the values in an array.
- This results in an array with its elements as node values.
- Traverse the array elements. When you encounter
f
(starting node), set counter to 0. - Keep traversing the array and check that:
- If the
g
(end node) is encountered, then stop traversing. - If the counter has been set, and the end node is not encountered, then update the value of the counter by 1.
- If the
- Finally, print the value of the counter.
Update:
As pointed out by anand_v.singh, if the tree might not be completely filled at all levels, then it can produce wrong results.
To overcome this problem, an additional parameter called tree_height
will be determined. Suppose, the height of the tree was 3, then the array will contain at most 2tree_height -1 elements, all initialized to a value that is not equal to the value of any tree nodes.
Now, you can use something like array representation of binary heap, to place node values into the array, at their respective indices. Then follow the above-mentioned procedure to obtain the results.
回答2:
Here is the code:
/**
*
* @author satish hawalppagol
*
*/
public class BinaryTreeTest
{
public int findDistance(Node root, int n1, int n2)
{
int leftNodeToRootNode = Pathlength(root, n1, "leftNodeToRootNode") - 2;
int rightNodeToRootNode = Pathlength(root, n2,"rightNodeToRootNode") - 2;
int lcaData = findLCA(root, n1, n2).data; //LCA->Lowest Common Ancestor
int lcaDistance = Pathlength(root, lcaData,"lcaDistance") - 1;
return (leftNodeToRootNode + rightNodeToRootNode) - 2 * lcaDistance;
}
public int Pathlength(Node root, int n1,String callingFrom)
{
if (root != null)
{
int x = 0;
if("rightNodeToRootNode" == callingFrom)
{
if(root.left ==null && root.right ==null)
{
//do nothing
}
else if(root.left ==null || root.right ==null)
{
System.out.println("counting the position where the node is not
present is : " + root.data);
}
if ((root.data == n1) || (x = Pathlength(root.left,
n1,"rightNodeToRootNode")) > 0 || (x = Pathlength(root.right,
n1,"rightNodeToRootNode")) > 0)
{
return x + 1;
}
}
if("rightNodeToRootNode" != callingFrom )
{
if ((root.data == n1) || (x = Pathlength(root.left,
n1,"leftNodeToRootNode")) > 0 || (x = Pathlength(root.right,
n1,"leftNodeToRootNode")) > 0)
{
return x + 1;
}
}
return 0;
}
return 0;
}
public Node findLCA(Node root, int n1, int n2)
{
if (root != null)
{
if (root.data == n1 || root.data == n2)
{
return root;
}
Node left = findLCA(root.left, n1, n2);
Node right = findLCA(root.right, n1, n2);
if (left != null && right != null)
{
return root;
}
if (left != null)
{
return left;
}
if (right != null)
{
return right;
}
}
return null;
}
public static void main(String[] args) throws java.lang.Exception
{
Node root = new Node(5);
root.left = new Node(2);
root.right = new Node(3);
/*root.left.right = new Node(12);*/
root.left.left = new Node(7);
root.left.left.left = new Node(9);
/*root.left.left.right = new Node(17);*/
root.right.right = new Node(1);
/*root.right.right.left = new Node(4);*/
root.right.right.right = new Node(6);
BinaryTreeTest binaryTreeTest = new BinaryTreeTest();
System.out.println("Distance between 9 and 6 is : " +
binaryTreeTest.findDistance(root,9, 6));
}
}
class Node
{
int data;
Node left;
Node right;
public Node(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
///////////input/////////
// 5 //
// / \ //
// 2 3 //
// / \ \ //
// 7 1 //
// / \ / \ //
// 9 6 //
///////////input/////////
counting the position where the node is not present is : 2
counting the position where the node is not present is : 7
counting the position where the node is not present is : 3
counting the position where the node is not present is : 1
Distance between 9 and 6 is : 4
来源:https://stackoverflow.com/questions/54815905/how-to-find-the-horizontal-distance-between-two-nodes-at-the-same-level-in-a-bin