0563.cpp (387B)
1 class Solution { 2 int res = 0; 3 4 int sum_adding_tilt(TreeNode *root) { 5 if (!root) return 0; 6 int l = sum_adding_tilt(root->left); 7 int r = sum_adding_tilt(root->right); 8 res += abs(l - r); 9 return l + r + root->val; 10 } 11 12 public: 13 int findTilt(TreeNode *root) { 14 res = 0; 15 sum_adding_tilt(root); 16 return res; 17 } 18 };