Binary Tree Diameter with DFS and Recursion

Day 32/100 – Diameter of Binary Tree Today’s problem looked simple on the surface, but it really tested my understanding of tree traversal and recursion. Key Insight: The diameter of a binary tree isn’t about paths from root—it’s about the longest path between any two nodes. The trick? At every node, calculate: Left subtree height Right subtree height Update diameter = left + right This turns a potentially complex problem into a smooth DFS + height calculation. What I learned: How to combine recursion with global state Thinking beyond root-based solutions Every node can be the "center" of the longest path Time Complexity: O(n) Space Complexity: O(h) Consistency > intensity. On to Day 33 #100DaysOfCode #DataStructures #Algorithms #LeetCode #Python #CodingJourney

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories