Untitled

 avatar
unknown
plain_text
a month ago
691 B
2
Indexable
//T.C : O(n)
//S.C : O(height of tree due to recursion)
class Solution {
    public static int distributeCandy(Node root) {
        if (root == null || (root.left == null && root.right == null)) {
            return 0;
        }

        int[] moves = {0};
        solve(root, moves);

        return moves[0];
    }
    private static int solve(Node root, int[] moves) {
        if (root == null) {
            return 0;
        }

        int l = solve(root.left, moves);
        int r = solve(root.right, moves);

        int totalExtraCandies = (l + r + root.data) - 1;

        moves[0] += Math.abs(l) + Math.abs(r);

        return totalExtraCandies;
    }
}
Leave a Comment