# Finding Minimum Steps in Special Binary Tree

## Introduction

Basic data structures that are frequently utilized in computer science and programming are binary trees. A unique kind of binary tree that is frequently seen is one where every node has an additional pointer to its parent. A binary tree with parent pointers, or special binary tree, is a type of tree data structure in which every node has a link to its parent. Easy navigation is made possible by the additional pointer to the parent, which is very helpful in several methods and situations.

Structure for a node in a special binary tree

### Algorithm

• Using the parent pointers to navigate up the tree, determine each node's depth.
• Go up the tree from the deeper node until both nodes are at the same depth.
• Go up from each node until you find a common ancestor.
• The total depths that remain after reaching the common ancestor are the minimal steps.

Code

Output:

Code Explanation

Node Structure

• The struct Node defines a binary tree node's fundamental structure.
• Every node has an integer value, pointers to its parent node (parent), and pointers to its left and right child nodes.

findDepth Function

• By going up through the parent pointers of a particular node, the findDepth function determines the depth of that node in the unique binary tree.
• The depth is increased by one each time iterating until the parent reference is NULL, initializing a depth variable to 0.
• The ultimate profundity is obtained back.

findMinimumSteps Function

• The function findMinimumSteps computes the least number of steps needed to go from one node to another given two input nodes, nodes 1 and 2.
• To find the depths of both nodes, it makes use of the findDepth function.
• The function then traverses up the tree and modifies the nodes to bring them to the same depth.
• It keeps on until a common ancestor is discovered.
• Lastly, it determines the least number of steps required by adding up all the depths that are left.

Main Function

• For testing reasons, a sample special binary tree is constructed in the main method.
• Malloc is used to allocate memory to the nodes.
• Sample data is used to set up the tree structure, and parent pointers are assigned appropriately.
• Two nodes (nodes 1 and 2) are sent to the findMinimumSteps function, and the output is reported.

Memory Cleanup

• To prevent memory leaks and release dynamically allocated memory, the code incorporates free memory cleanup.
• Every allotted node is released in the opposite sequence in which it was created, beginning with the leaves and progressing toward the root.

Time and Space Complexity

The `findDepth` and `findMinimumSteps` functions are the main factors that influence the code's temporal complexity. The `findDepth` method has an O(h) time complexity since it traverses the parent pointers from a particular node to the root. h is the tree's height. Similar to this, the `findMinimumSteps` function has an O(h) time complexity since it searches up the tree until a common ancestor is discovered. h is the tree's height. The temporal complexity is often efficient as the tree grows more balanced, with h approaching log(n) for n nodes. Due to the constant memory usage that does not increase with input size, the space complexity is O(1).