The diameter of a binary tree is defined as the length of the longest path between any two nodes in the tree. This path may or may not pass through the root node.
To calculate the diameter of a binary tree, we first find the heights of the left and right subtrees of each node. The diameter of a node is then the sum of the heights of its left and right subtrees, plus one (to include the node itself). We recursively calculate the diameter of each node and keep track of the maximum diameter found so far. Finally, we return the maximum diameter as the diameter of the entire tree.
In other words, the diameter of a binary tree is the maximum of:
- The diameter of the left subtree
- The diameter of the right subtree
- The height of the left subtree plus the height of the right subtree plus one (to include the root node)
Note that the height of a binary tree is the maximum number of edges between the root node and any leaf node in the tree.
class TreeNode(var value: Int) {
var left: TreeNode? = null
var right: TreeNode? = null
}
fun diameterOfBinaryTree(root: TreeNode?): Int {
var diameter = 0
fun height(node: TreeNode?): Int {
if (node == null) {
return 0
}
val leftHeight = height(node.left)
val rightHeight = height(node.right)
diameter = maxOf(diameter, leftHeight + rightHeight)
return 1 + maxOf(leftHeight, rightHeight)
}
height(root)
return diameter
}
- We define a
TreeNode
class to represent a node in the binary tree. Each node has avalue
,left
child node, andright
child node. - We define the
diameterOfBinaryTree
function, which takes a single input parameter,root
, which is the root node of the binary tree. - We initialize a variable
diameter
to 0, which will hold the maximum diameter found so far. - We define a local function
height
, which calculates the height of a node and updates thediameter
variable if the diameter of the node is greater than the maximum diameter found so far. The function returns the height of the node. - In the
height
function, we first check if the node is null. If it is, we return 0. - We recursively calculate the heights of the left and right subtrees of the node by calling the
height
function on theleft
andright
child nodes, respectively. - We update the
diameter
variable by taking the maximum of the current value ofdiameter
and the sum of the heights of the left and right subtrees of the node. - We return the height of the node as 1 plus the maximum of the heights of the left and right subtrees.
- Finally, we call the
height
function with the root node of the binary tree, and return the value of thediameter
variable as the diameter of the binary tree.
class TreeNode(var value: Int) {
var left: TreeNode? = null
var right: TreeNode? = null
}
fun diameterOfBinaryTree(root: TreeNode?): Int {
var diameter = 0
fun height(node: TreeNode?): Int {
if (node == null) {
return 0
}
val leftHeight = height(node.left)
val rightHeight = height(node.right)
diameter = maxOf(diameter, leftHeight + rightHeight)
return 1 + maxOf(leftHeight, rightHeight)
}
height(root)
return diameter
}
fun main() {
/*
* Create a binary tree:
*
* 1
* / \
* 2 3
* / \
* 4 5
*
* The diameter of this tree is 3, which is the length of the path from node 4 to node 5.
*/
val root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left?.left = TreeNode(4)
root.left?.right = TreeNode(5)
val diameter = diameterOfBinaryTree(root)
println("The diameter of the binary tree is $diameter")
}