Given the root of a Binary Tree and an integer that represents the **distance** is defined as the minimum number of **nodes** that must be traversed to reach the target node.

**Example: **

`data`

value of a `TreeNode`

present in the tree, write a method - `pathLengthFromRoot`

that returns the distance between the root and that node. You can assume that the given key exists in the tree. The 1

/ \

2 3

\ \

4 5

pathLengthFromRoot(root,5) => 3

pathLengthFromRoot(root,1) => 1

pathLengthFromRoot(root,3) => 2

Need a **hand?** Try out these hints, one at a time.

This problem has a fairly simple and concise recursive implementation. There are multiple terminating conditions to keep track of, however. Try and limit your solution to as few lines as possible.

1) Start with the obvious terminating condition :

2) Initialize the return variable :

3) You can write the various recursions in one elegant if block that uses

4) Finally, if none of the conditions are met, simply return 0.

if (root == null) return 0;

2) Initialize the return variable :

`int out = 0;`

3) You can write the various recursions in one elegant if block that uses

`short-circuiting`

to only evaluate additional checks if needed : if ((root.data == n1) || (out = pathLengthFromRoot(root.left, n1)) > 0 || (out = pathLengthFromRoot(root.right, n1)) > 0) {

return out + 1;

}

4) Finally, if none of the conditions are met, simply return 0.

public int pathLengthFromRoot(TreeNode root, int n1) { }

**C**

**Java**

**Python**