Rotate image (square matrix) by 90 deg
Tue, 06 May 2025
Given a Binary Tree and a Node,
Write a function that return the vertical distance of that node from root of the tree. The vertical distance of a node is computed as below:

If we could not find the node that we are looking for, then we return some value to indicate that we did not find the node (in this example, I am using INT_MIN, assuming that this value will never be a valid value).
Code:
// Return the vertical distance of node with value v
// from root of the tree pointed to by r.
// distance is initially 0.
int verticalDistance(Node* r, int v, int distance)
{
if(r == NULL){
return INT_MIN;
}
if(r->data == v)
return distance;
int dist = verticalDistance(r->left, v, distance-1);
// Element found in left tree.
if(dist != -1)
return dist;
return verticalDistance(r->right, v, distance+1);
}
If INT_MIN is a valid value of the Vertical Distance, then we may have to return two values (a pair) from the function 1. A Boolean value indicating whether or no the value is found in the tree.
2. If the first return value is true, then this second value returns the Vertical Distance.
Tue, 06 May 2025
Tue, 06 May 2025
Tue, 06 May 2025
Leave a comment