Satellite

in #ita29 days 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));
}

}

Sort:  

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.

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.032
BTC 61660.23
ETH 3056.45
USDT 1.00
SBD 3.82