0
0
DSA Typescriptprogramming~30 mins

Diameter of Binary Tree in DSA Typescript - Build from Scratch

Choose your learning style9 modes available
Diameter of Binary Tree
📖 Scenario: You are working on a network system where each node represents a server. The longest path between any two servers in the network is important to understand the maximum delay in communication.This network is represented as a binary tree where each node can have up to two connections (left and right child nodes).
🎯 Goal: Calculate the diameter of the binary tree, which is the length of the longest path between any two nodes in the tree.You will build a TypeScript program that defines the tree, sets up a helper variable, implements the diameter calculation, and finally prints the diameter.
📋 What You'll Learn
Define a binary tree node class with val, left, and right properties
Create a sample binary tree with exactly 5 nodes with given values
Use a helper variable to track the maximum diameter found during traversal
Implement a recursive function to calculate the diameter of the binary tree
Print the final diameter value
💡 Why This Matters
🌍 Real World
Understanding the longest path in a network or tree structure helps in optimizing communication delays and resource allocation.
💼 Career
This concept is useful for software engineers working on network design, distributed systems, and performance optimization.
Progress0 / 4 steps
1
Define the Binary Tree and Create the Sample Tree
Define a class called TreeNode with a constructor that takes a number val and initializes left and right as null. Then create a binary tree with root node value 1, left child 2, right child 3, left child of node 2 as 4, and right child of node 2 as 5.
DSA Typescript
Hint

Start by defining the TreeNode class with val, left, and right. Then create the nodes and link them as described.

2
Add a Helper Variable to Track Maximum Diameter
Create a variable called maxDiameter and initialize it to 0. This will keep track of the longest path found during the tree traversal.
DSA Typescript
Hint

Just create a variable maxDiameter and set it to zero before starting the diameter calculation.

3
Implement the Diameter Calculation Function
Write a recursive function called depth that takes a TreeNode | null parameter called node. It should return the maximum depth of the subtree rooted at node. Inside the function, calculate the left and right depths by calling depth recursively. Update maxDiameter with the sum of left and right depths if it is larger than the current maxDiameter. Return the maximum of left and right depths plus one.
DSA Typescript
Hint

Use recursion to find the depth of left and right subtrees. Update maxDiameter with the sum of these depths if it is larger. Return the max depth plus one.

4
Print the Diameter of the Binary Tree
Write a console.log statement to print the value of maxDiameter.
DSA Typescript
Hint

Use console.log(maxDiameter) to display the diameter after the recursive calls.