Leetcode | 563. 二叉树的坡度

题目:563. 二叉树的坡度

测试代码:

 1 class Solution {
 2 public:
 3     int countChild(TreeNode* node) {
 4         if (node == nullptr) return 0;
 5         return node->val + countChild(node->left) + countChild(node->right);
 6     }
 7 
 8     int findTilt(TreeNode* root) {
 9         if (root == nullptr) return 0;
10         return abs(countChild(root->left) - countChild(root->right)) + findTilt(root->left) + findTilt(root->right);
11     }
12 };

猜你喜欢

转载自www.cnblogs.com/sunbines/p/9670077.html