Solved Add One Row to Tree on LeetCode with DFS

Day 93 of #100DaysOfCode Today I solved "Add One Row to Tree" on LeetCode using a DFS approach. Key Idea: We need to insert a new row of nodes at a given depth, while preserving the existing structure below it. Approach: • If depth == 1: Create a new root and attach the original tree as its left child • Otherwise: Traverse the tree using DFS When we reach level depth - 1: Create new nodes with given value Attach original left subtree to new left node Attach original right subtree to new right node Concepts Used: • Binary Trees • Depth First Search (DFS) • Recursion • Tree modification Time Complexity: O(n) Space Complexity: O(h) This problem was a great exercise in modifying tree structure while maintaining connections correctly Not just traversing trees now… actually reshaping them #Day93 #100DaysOfCode #LeetCode #BinaryTree #DFS #Cpp #CodingJourney

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories