Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Reconstruct Binary Tree With Levelorder And Inorder leetcode

public TreeNode reconstruct(int[] in, int[] level) {
    if (in.length == 0) {
      return null;
    }
    Map<Integer, Integer> hashmap = new HashMap<>();
    List<Integer> levell = new ArrayList<>();
    for (int i = 0; i < in.length; i++) {
      hashmap.put(in[i], i);
      levell.add(level[i]);
    }
    return build(in, 0, in.length - 1, levell, hashmap);
  }
  private TreeNode build(int[] in, int instart, int inend, List<Integer> level, Map<Integer, Integer> hashmap) {
    if (instart > inend) {
      return null;
    }
    TreeNode root = new TreeNode(level.get(0));
    // int position = find(inorder, instart, inend, post[postend]);
    List<Integer> left = new ArrayList<>();
    List<Integer> right = new ArrayList<>();
    int position = hashmap.get(level.get(0));
    for (int i = 1; i < level.size(); i++) {
      int num = level.get(i);
      int pos = hashmap.get(num);
      if (pos < position) {
        left.add(num);
      } else {
        right.add(num);
      }
    }
    root.left = build(in, instart, position - 1, left, hashmap);
    root.right = build(in, position + 1, inend, right, hashmap);
    return root;
  }
Comment

PREVIOUS NEXT
Code Example
Java :: Map - counting with map 
Java :: How do you make bedrock full screen in Minecraft? 
Java :: java truncate bigdecimal 
Java :: output of this 
Java :: log.d() andriod 
Java :: intellij error for new project 
Java :: exoplayer how to put loader while video is still loading android java 
Java :: search and delete class files from jars 
Java :: empty map entry java 
Java :: spring boot initializr-generator example 
Java :: java modulus opperation 
Java :: restore 
Java :: java default access modifier 
Java :: Deque interface in Java 
Java :: reactive kafka 
Java :: open google maps cycling navigation intent 
Java :: Using Looping Construct to Copy Arrays Java 
Java :: PlatformException (PlatformException(unknown, java.lang.Exception: Client is offline, {code: unknown, message: java.lang.Exception: Client is offline}, null)) 
Java :: cgange background from button click java fx 
Java :: Android java parse class name via intent 
Java :: read only jtextfield 
Java :: how to preset an array java 
Java :: sort a list according to location 
Java :: Java Protected Access Modifier package two 
Java :: lcm 
Java :: java code to get all leaf nodes of a xml file 
Java :: how to get single value from input string in java 
Java :: compile option in android 
Java :: antlr TestRig in java program 
Java :: zebra zpl print java 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =