LeetCode563. 二叉树的坡度

题目

 1 class Solution {
 2 public:
 3     int ans = 0;
 4     int findTilt(TreeNode* root) {
 5         postOrder(root);
 6         return ans;  
 7     }
 8     int postOrder(TreeNode* root){
 9         if(root == NULL) return 0;
10         int leftSum = postOrder(root->left);
11         int rightSum = postOrder(root->right);
12         ans += abs(leftSum - rightSum);
13         return root->val + leftSum + rightSum;
14     }
15 };

 

posted @ 2021-01-10 16:37  Uitachi  阅读(54)  评论(0编辑  收藏  举报