Depth First Profile Image
Depth First
2 months ago
0
1.1K
1
49
Diameter of a Binary Tree - LeetCode 543 The diameter of a binary tree is the number of edges in the longest path between any two nodes. That path does not have to pass through the root. At every node, we calculate the height of its left and right subtrees. Adding those two heights gives us the diameter passing through that node. We compare this with the largest diameter found so far, then return the greater subtree height plus one to the parent. The important idea is that both branches can form the diameter, but only one branch can continue upward through recursion. Time complexity: O(n) Space complexity: O(h) #neetcode150 #datastructuresandalgorithms #codinginterview #neetcode #binary#computerscienceengineering
Download video without watermark
original sound - DepthFirst
•
169s