• + 1 comment

    somehow my output isnt right

    import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
       Scanner scan = new Scanner(System.in);
        Solution s = new Solution();
        long n = scan.nextInt();
        long m = scan.nextInt();
        final double modConst = Math.pow(10,9)+7;
        Node node = null;
        Node root = null;
        int id = 1;
        for(int i=0;i<n-1;i++){
            if(node == null){
                node = new Node(scan.nextLong());
                node.id = ++id;
                root = node;
            } else if( node.left == null) {
                node.left = new Node(scan.nextLong());
                node.left.parent = node;
                node.left.id = ++id;
            } else if( node.right == null) {
                node.right = new Node(scan.nextLong());
                node.right.id = ++id;
                node.right.parent = node;
                node = node.left;
            }
        }
        if(node.left == null){
            node.left = new Node(0);
            node.left.parent = node;
            node.left.id = 1;
        } else if (node.right == null){
            node.right = new Node(0);
            node.right.parent = node;
            node.right.id = 1;
        }
        List<List<String>> queries = new ArrayList<List<String>>();
        List<String> query = null;
        for( int i=0;i<m;i++){
            query = new ArrayList<String>();
            for(int j = 0; j < 3;j++){
               query.add(scan.next());
            }
            queries.add(query);
        }
        long nNum,rNum, sum = 0;
        for(List<String> task : queries) {
            nNum = Long.parseLong(task.get(1));
            rNum = Long.parseLong(task.get(2));
           if(task.get(0).equalsIgnoreCase("U")){
               //s.printTree(root);
               s.updateOperation(nNum,rNum,root);
              // s.printTree(root);
           } else {
              s.printTree(root);
             sum = s.queryOperation(nNum,rNum,root);
             System.out.println( sum % modConst );
    
             System.out.println("\n\n\n");
    
           }
        }
    
       // System.out.println(s.fetchTreeSize(root));
    
    }
    
    private Node findNode(long nodeNumber, Node rootNode){
         if(rootNode == null){
            return null;
        } 
    
        if(rootNode.id != nodeNumber){
            findNode(nodeNumber,rootNode.left );
            findNode(nodeNumber,rootNode.right );
            return rootNode;
        } else {
            return rootNode;
        }
    }
    
    private long queryOperation(long xNum,long yNum,Node rootNode){
        Node yNode = findNode(yNum, rootNode);
        long sum = 0;
        System.out.println("yNode Val:"+yNode.value+" yNum:"+yNum);
        while(yNode.parent != null && yNode.parent.id != xNum){
            sum = sum + yNode.value;
    
            if(yNode.parent == null){
                break;
            }
            yNode = yNode.parent;
        }
        return sum ;
    
    }
    
    private void updateOperation(long nodeNumber, long kfibo, Node rootNode) {
    
          rootNode = findNode(nodeNumber,rootNode);
          long treeElements = fetchTreeSize(rootNode);
          UpdateTree(rootNode,kfibo,0);
    
    }
    
    private void printTree(Node rootNode){
        if(rootNode == null ) {
            return;
        }
    
        printTree(rootNode.left);
        System.out.println("id :"+rootNode.id+" value :"+rootNode.value);
         printTree(rootNode.right);
    }
    
    private void UpdateTree(Node updNode, long item,int level){
        if(updNode == null){
            return;
        }
        updNode.addValue(fetchFibonaciiValue(item+level));
        UpdateTree(updNode.left,item,++level);
        UpdateTree(updNode.right,item,level);
    }
    
    
    private long fetchFibonaciiValue(long element){
        if(element == 1 || element == 2) {
            return 1;
        } else if(element < 1){
            return 0;
        } else {
            return fetchFibonaciiValue(element-1) + fetchFibonaciiValue(element -2);
        }
    }
    
    private long fetchTreeSize(Node rootNode){
        if(rootNode == null){
            return 0;
        } else {
            return 1 + fetchTreeSize(rootNode.left)+fetchTreeSize(rootNode.right);
        }
    }
    

    } class Node { Node left = null; Node right = null; Node parent = null; long value = 0; long id = 0; Node(long val) { this.value = val; this.left = null; this.right = null; }

    public void addValue(long p) {
        this.value = value + p;
    }
    

    }