I've been working on algorithm problems in my free time as a way to prep for interviews for summer internships next summer. I'm currently working on tree problems, and I've found that the actual coding is pretty trivial for most problems, but I have a hard time understanding the complexity of these problems. A problem I was working on is Binary tree Preorder Traversal, and I implemented it both recursively and iteratively. However, the iterative solution was much slower, and I don't understand why? here are my two solutions:
Iterative:
public List<Integer> preorder(TreeNode root) {
List<Integer> ans = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if (root == null) return ans;
stack.push(root);
while(!stack.isEmpty()) {
TreeNode node = stack.pop();
ans.add(node.val);
if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
{
return ans;
}
Recursive:
public list<Integer> preorder(TreeNode root) {
List<Integer> ans = new ArrayList<>();
dfs(root, ans);
return ans;
}
public void dfs (TreeNode root, List<Integer> ans) {
if (root == null) return;
ans.add(root.val);
dfs(root.left);
dfs(node.right)
}
As far as I can see, the recursive solution has two branches per call, and the depth of the tree if balanced is O(log n), however worst case is O(n), so the complexity would be O(2^n). I could be completely off and probably am. The iterative solution has constant push and pop time, so my initial intuition was that it would be O(n) time. If these were the circumstances, I can't see how the exponential solution would run faster. If somebody could help me understand the complexities of such problems that would be greatly appreciated.