Find the diameter of a binary tree

前端 未结 7 1093
旧时难觅i
旧时难觅i 2021-01-01 03:01

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:

7条回答
  •  悲&欢浪女
    2021-01-01 03:35

    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.

提交回复
热议问题