Skip to content

Complete Trees-2 Assignment - #1602

Open
tejbharath wants to merge 2 commits into
super30admin:masterfrom
tejbharath:master
Open

Complete Trees-2 Assignment#1602
tejbharath wants to merge 2 commits into
super30admin:masterfrom
tejbharath:master

Conversation

@tejbharath

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Construct Binary Tree from Inorder and Postorder Traversal (InOrderPostOrderBinaryTree.java)

E.g., INCORRECT - The solution doesn't solve the postorder problem; it solves the preorder+inorder problem instead.

INCORRECT - The solution doesn't solve the postorder problem; it solves the preorder+inorder problem instead.

VERDICT: NEEDS_IMPROVEMENT


Sum Root to Leaf Numbers (SumRootToLeaf)

Great work! Your solution is correct, efficient, and well-structured. Here are some minor suggestions:

  1. Space Complexity: Your analysis states O(n), but the actual space complexity is O(h), where h is the height of the tree. This is because the recursion stack depth depends on the height of the tree, not the total number of nodes. In the worst case (a completely skewed tree), h = n, so O(n) is technically correct as an upper bound, but O(h) is more precise and informative.

  2. Field Initialization: While Java automatically initializes instance int fields to 0, it's a best practice to explicitly initialize them, especially for fields that are modified by methods. Consider initializing result to 0 in the constructor or at declaration:

    int result = 0;
  3. Code Style: Your code is clean and well-commented. The approach comment at the top is a nice touch. Keep up the good work!

  4. Edge Cases: Consider what happens if the root is null. Your current solution would return 0 (since result is 0 and the helper returns immediately), which is correct behavior. However, you might want to add a null check at the beginning of sumNumbers for clarity:

    if (root == null) return 0;

VERDICT: PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants