-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
42 lines (35 loc) · 1.02 KB
/
Copy pathsolution.cpp
File metadata and controls
42 lines (35 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
class Node {
public:
int data;
Node* left;
Node* right;
Node(int x) {
data = x;
left = right = nullptr;
}
};
*/
#include <bits/stdc++.h>
using namespace std;
class Solution {
private:
long long moves = 0; // accumulate total moves (use long long for safety)
// returns balance of subtree:
// positive -> this subtree has extra candies to send up
// negative -> this subtree needs candies from above
int dfs(Node* node) {
if (!node) return 0;
int lb = dfs(node->left); // balance from left subtree
int rb = dfs(node->right); // balance from right subtree
moves += llabs((long long)lb) + llabs((long long)rb); // moves needed to fix edges to children
int balance = node->data + lb + rb - 1; // current subtree balance after giving node exactly 1 candy
return balance;
}
public:
int distCandy(Node* root) {
moves = 0;
dfs(root);
return (int)moves;
}
};