Code Interview Note
  • 0. Introduction
  • 1. Basic
    • Python Basic
    • Java Basic
    • Primitive Type
    • Basic Question
    • Number
  • 2. Array and Numbers
    • General
    • TwoSum
    • Buy and Sell Stock
    • SubArray
      • SubArray + HashMap
    • Sliding Window
      • Sliding Window At Most Problem
    • Word Break
    • Passes Problem
    • Majority Element
    • Partition Array
    • Sort Colors
    • Anagram
    • Ugly Number
    • TwoPointer
    • Swipe Line
    • Calculator
    • Sudoku
  • 2.1 String
    • String
    • Palindrome
    • Parentheses
    • Decode String
    • Calculator
    • Abbreviation
  • 3. Linkedlist
    • Dummy Node
    • Double Pointers
  • 4. Stack and Queue
    • General
    • Increase/Decrease Stack
  • 5. Binary Search
    • General
    • BS on result
    • Save the half which has result
    • Rotated Sorted Array
    • Split Array Largest Sum
  • 6. Binary Tree
    • General
    • Path Sum
    • Lowest Common Ancestor
    • BST
    • Convert
    • Traverse
    • Valid Ordered Tree
    • Construct Binary Tree
    • Tree depth and width
    • Vertical Order Traverse
  • 7. Heap
    • Geneal
    • TopK
  • 8. Simulation
    • General
    • Read4
    • Encode Decode
    • LRU/LFU
    • Robot
    • GetRandom O(1)
    • Probability
  • 9. DFS
    • Backtrack
    • General
    • Subset
    • Permutation
    • Combination
  • 10. HashTable
    • General
  • 11. Sort
    • General
  • 12. Recursion
    • General
  • 13. Dynamic Programming
    • Graph
    • General
    • Coordinate
    • Double Sequence
    • Longest Common Subsequence
    • Rolling Array
    • House Robber
    • Backpack
    • Memorization
    • Diagonal
  • 14. BFS
    • General
    • Number of Islands
    • The Maze
  • 15. Graph
    • Shortest Path
    • Undirected Graph
    • Topology Sort
    • Word Ladder
    • Tarjan's Algo
  • 16. Divide & Conquer
    • General
  • 17. UnionFind
    • General
    • Grouping
  • 18. Trie
    • General
    • Word Square
  • 19. Company Summary
    • Oracle
    • Amazon
      • DP
    • Google
    • Hackerrank
    • LinkedIn
  • 20. Design
  • 21. Math
  • Behavior Question
  • Internet
  • OS
Powered by GitBook
On this page
  • 105. Construct Binary Tree from Preorder and Inorder Traversal
  • 106. Construct Binary Tree from Inorder and Postorder Traversal
  • 889. Construct Binary Tree from Preorder and Postorder Traversal

Was this helpful?

  1. 6. Binary Tree

Construct Binary Tree

105. Construct Binary Tree from Preorder and Inorder Traversal

 public TreeNode buildTree(int[] preorder, int[] inorder) {
    if(preorder.length == 0) return null;

    int num = preorder[0];
    int len = 0;
    for(int i=0; i<inorder.length; i++){
        if(inorder[i] == num)
            len = i;
    }

    TreeNode node = new TreeNode(num);
    node.left = buildTree(Arrays.copyOfRange(preorder, 1, 1+len),
                         Arrays.copyOfRange(inorder, 0, len));
    node.right = buildTree(Arrays.copyOfRange(preorder, 1+len, preorder.length),
                        Arrays.copyOfRange(inorder, 1+len, inorder.length));
    return node;
}

106. Construct Binary Tree from Inorder and Postorder Traversal

public TreeNode buildTree(int[] inorder, int[] postorder) {
    if(postorder.length == 0) return null;

    int n = postorder[postorder.length-1];
    int len = 0;
    for(int i=0; i<inorder.length; i++){
        if(inorder[i] == n) len = i;
    }

    TreeNode root = new TreeNode(n);
    root.left = buildTree(Arrays.copyOfRange(inorder, 0, len), 
                          Arrays.copyOfRange(postorder, 0, len));
    root.right = buildTree(Arrays.copyOfRange(inorder, len+1, inorder.length),
                           Arrays.copyOfRange(postorder, len, postorder.length-1));
    return root;
}

889. Construct Binary Tree from Preorder and Postorder Traversal

Return any binary tree that matches the given preorder and postorder traversals.

Example

Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1] Output: [1,2,3,4,5,6,7]

  • find the length of left tree and use it split the left tree and right tree

  • Preorder and postorder left tree length are the same, and pre[1] the head of left tree, use is to find the last position in postorder

public TreeNode constructFromPrePost(int[] pre, int[] post) {
    // pre  [1][2,4,5][3,6,7]
    // post [4,5,2][6,7,3][1]

    int len = pre.length;
    if(len == 0) return null;
    TreeNode node = new TreeNode(pre[0]);
    if(len == 1) return node;

    // find out the length of left tree
    int left_len = 0;
    for(int i=0; i<len; i++){
        if(post[i] == pre[1]) left_len = i+1;
    }

    node.left = constructFromPrePost(Arrays.copyOfRange(pre, 1, 1+left_len), 
                                        Arrays.copyOfRange(post, 0, left_len));
    node.right = constructFromPrePost(Arrays.copyOfRange(pre, 1+left_len, len),
                                         Arrays.copyOfRange(post, left_len, len-1));
    return node;
}
PreviousValid Ordered TreeNextTree depth and width

Last updated 5 years ago

Was this helpful?