Tuesday, February 25, 2014

Given a BST, replace each node with the sum of the values of all the nodes that are greater than that node.

public static int chSum(TNode node){
        if(node == null){
            return 0;
        }else if(node.left == null && node.right == null){
            return 0;  // return 0,since we dont have anything left
        }else{
            int  tmp = node.value;
            node.value = chSum(node.right);
            return tmp + node.value + chSum(node.left);
        }
    }

No comments:

My Profile

My photo
can be reached at 09916017317