I am trying to find the diameter of a binary tree (Path length between any two nodes in the tree containing maximum number of nodes.) in java.
my code snippet:
You find a diameter of a tree by running a BFS from any node and then another BFS from the furthest node(the node last visited during the first BFS). The diameter is formed by the node last visited in the first BFS and the node last visited in the first BFS. The fact that the tree is binary does not affect the algorithm.
EDIT: changing the value of d
in the code you have written will not affect the argument you pass as primitive types are not passed by reference in java.