Skip to content

Create trees2.java - #1599

Open
Praniksha123 wants to merge 1 commit into
super30admin:masterfrom
Praniksha123:master
Open

Create trees2.java#1599
Praniksha123 wants to merge 1 commit into
super30admin:masterfrom
Praniksha123:master

Conversation

@Praniksha123

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

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

Your solution is correct and demonstrates a good understanding of the recursive approach. However, there are significant efficiency concerns:

  1. Time Complexity Issue: Your solution runs in O(n²) time due to the linear search through inorder at each recursive call and array copying. The optimal solution uses a HashMap for O(1) lookup.

  2. Space Complexity Issue: Creating new arrays at each recursion level uses O(n²) space. Use index pointers instead.

  3. Suggested Optimization:

class Solution {
    Map<Integer, Integer> inorderMap;
    int postIdx;
    
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        inorderMap = new HashMap<>();
        postIdx = postorder.length - 1;
        for (int i = 0; i < inorder.length; i++) {
            inorderMap.put(inorder[i], i);
        }
        return build(inorder, postorder, 0, inorder.length - 1);
    }
    
    private TreeNode build(int[] inorder, int[] postorder, int start, int end) {
        if (start > end) return null;
        int rootVal = postorder[postIdx--];
        int rootIdx = inorderMap.get(rootVal);
        TreeNode root = new TreeNode(rootVal);
        root.right = build(inorder, postorder, rootIdx + 1, end);
        root.left = build(inorder, postorder, start, rootIdx - 1);
        return root;
    }
}
  1. File Organization: Please submit only the solution for the problem being evaluated. The file contains an unrelated solution which is confusing.

  2. Strengths: Your recursive logic is correct, and the code is readable. The base case handling is proper.

VERDICT: NEEDS_IMPROVEMENT


Sum Root to Leaf Numbers

Strengths:

  • Your recursive approach correctly identifies the problem structure.
  • The solution handles all edge cases properly (null root, single node tree).
  • The code is readable and follows a logical flow.

Areas for Improvement:

  1. Use integer arithmetic instead of strings: Instead of building strings and parsing them, accumulate the number directly: currNum = currNum * 10 + root.val. This is more efficient and avoids potential overflow concerns.

  2. Remove dead code: The int sum=0; declaration in totalsum is never used - remove it.

  3. Simplify the entry point: The initial null check in sumNumbers is redundant since your recursive function handles null nodes. You could simplify to just return totalsum(root, 0); if you switch to integer accumulation.

  4. File organization: Consider putting each problem solution in its own file for better maintainability.

Suggested refactor:

class Solution {
    public int sumNumbers(TreeNode root) {
        return helper(root, 0);
    }
    
    private int helper(TreeNode root, int currNum) {
        if (root == null) return 0;
        currNum = currNum * 10 + root.val;
        if (

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