Iteration

Here's our original solution which is recursive. Try converting it to iterative code, where the function doesn't call itself.

First Few Test Cases:

To solve this, we want to create our own Call Stack, which just means storing a stack which contains all the inputs that we want to run treesEqual on.

We could solve this problem by visiting every input twice, but it's actually easier to just return False immediately if we see any trees that are not equal, and return True at the end otherwise.

Time Complexity O(n)O(n)

Space Complexity O(n)O(n). The stack grows in the same exact way as the recursive solution, so it has the same space complexity.

Mark as Completed:
Submits:
treesEqual
Imports:
TreeNode
Test your code to get an output here!
treesEqual(
)
Test your code to get an output here!