Satellite

in ita •  7 months ago 

import java.util.*;

public class Satellite {

private Node buildTree(int preId, List<Character> preorderInput, List<Character> inorderInput, int startId, int endId){
    if(startId > endId) return null;
    Node local = new Node(preorderInput.get(preId));
    if(startId == endId) return local;
    int index = startId;
    for(int i=startId; i<= endId;i++){
        if(local.value == inorderInput.get(i)) index = i;
    }
        
    local.left = buildTree(++preId,preorderInput, inorderInput, startId, index-1); 
    local.right = buildTree(++preId,preorderInput, inorderInput, index+1, endId); 
    return local;
    }

public Tree treeFromTraversals(List<Character> preorderInput, List<Character> inorderInput) {
    
    if(preorderInput.size() != inorderInput.size()) throw new IllegalArgumentException("traversals must have the same length");
    Set<Character> setVer = new HashSet<>(preorderInput);
    if(setVer.size() != preorderInput.size()) throw new IllegalArgumentException("traversals must contain unique items");
    // equals per verificare che due set abbiano gli stessi elmenti. 
    Set<Character> setVerIn = new HashSet<>(inorderInput);
    if(!setVer.equals(setVerIn)) throw new IllegalArgumentException("traversals must have the same elements");
    return new Tree(buildTree(0,preorderInput,inorderInput,0,preorderInput.size()-1));
}

}

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you, friend!
I'm @steem.history, who is steem witness.
Thank you for witnessvoting for me.
image.png
please click it!
image.png
(Go to https://steemit.com/~witnesses and type fbslo at the bottom of the page)

The weight is reduced because of the lack of Voting Power. If you vote for me as a witness, you can get my little vote.