Univalued Binary Tree: DFS Approach

🚀 Day 117 of #LeetCode Challenge! Problem: Univalued Binary Tree 💡 My Approach: We need to check if all nodes in the binary tree have the same value. 🔍 Steps: Store the root value as the target value DFS through every node If any node's value ≠ root value → return false If traversal completes with no mismatch → return true ✅ 📌 Key Idea: A binary tree is univalued if every node contains the same value. ✨ Example: Input: [1,1,1,1,1,null,1] Output: true Input: [2,2,2,5,2] Output: false ⏱ Time Complexity: O(N) — every node is checked 💾 Space Complexity: O(H) — recursion stack (H = tree height) 👨💻 GitHub Link: https://lnkd.in/g3xFQpAm #LeetCode #BinaryTree #DFS #Recursion #DSA #Day117 #C++ #ProblemSolving #CodingChallenge

  • text

To view or add a comment, sign in

Explore content categories