Friday, September 13, 2013

find a path having maximum sum of all node data in a given BST


    public int findMaxPath() {
        head = root;
        int maxPath = findMaxPath(root, 1);
        return maxPath;
    }

    public int findMaxPath(Node node, int max) {
        if (node == null) {
            return 0;
        }
        int lmax = getHeight(node.leftChild);
        int rmax = getHeight(node.rightChild);
        int path = lmax + rmax + 1;
        if (max < path) {
            max = path;
            head = node;
        }
        findMaxPath(node.leftChild, max);
        findMaxPath(node.rightChild, max);
        return max;
    }

No comments:

Post a Comment