Перейти к содержимому

Diameter of Binary Tree 🌳 | DFS & Recursion in C++ | DSA Series #59

ARKAPRAVA CHAKRABORTY

0:00 / 0:00

Diameter of Binary Tree 🌳 | DFS & Recursion in C++ | DSA Series #59

5 просмотров · 12 дней назад
ARKAPRAVA CHAKRABORTY
5 подписчиков
5 просмотров · 12 дней назад
Welcome to Day 59 of my DSA Journey! Today I solved Diameter of a Binary Tree in C++ using DFS + Recursion. The diameter of a binary tree is the longest path between any two nodes. The path doesn't necessarily have to pass through the root, which makes this a great problem for understanding recursive tree traversal. 💡 Key idea: For every node, calculate the height of the left and right subtrees. Diameter through current node = left height + right height While returning the height to the parent, we continuously update the maximum diameter found so far. 🔹 Recursively calculate left height 🔹 Recursively calculate right height 🔹 Update the maximum diameter 🔹 Return 1 + max(leftHeight, rightHeight) ⏱️ Time Complexity: O(n) 💾 Space Complexity: O(h), where h is the height of the tree 📚 Learning Resource (ChaiCode DSA): https://dsa.chaicode.com/signup?ref=N... If you found this video helpful: 👍 Like the video 💬 Share your thoughts in the comments 🔔 Subscribe for more DSA videos as I continue learning in public. #DSA #CPP #CPlusPlus #BinaryTree #DiameterOfBinaryTree #DFS #Recursion #LeetCode #Chaicode #Algorithms #DataStructures #CodingInterview #Programming #LearningInPublic