dsa1 min read
Diameter of Binary Tree — Tree DP Height Trick
Find the diameter (longest path between any two nodes) using a depth function that tracks the maximum path through each node.
Read →
webcoderspeed.com
1276 articles
Find the diameter (longest path between any two nodes) using a depth function that tracks the maximum path through each node.
Validate a BST by passing down min and max bounds through DFS, ensuring every node falls strictly within its valid range.
Find the kth smallest element in a BST using inorder traversal (left-root-right) which visits nodes in sorted order.
Find the LCA of two nodes using post-order DFS: return root when found, propagate upward and split at the ancestor.
Find the LCA in a BST in O(h) by navigating left or right based on whether both nodes are in the same subtree.