# 563.二叉树的坡度

<https://leetcode-cn.com/problems/binary-tree-tilt/>

## 一、后序

计算左右子树之和的差，考虑后序遍历。在遍历中计算本节点坡度并修改，用一个全局变量累加坡度，并向上层返回本子树节点和

```python
class Solution:
    def findTilt(self, root: TreeNode) -> int:
        self.res = 0

        def postOrder(root):
            if not root:
                return 0
            left = postOrder(root.left)
            right = postOrder(root.right)
            sum = left + right + root.val
            root.val = abs(left-right)
            self.res += root.val	#累加坡度
            return sum

        postOrder(root)
        return self.res
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cai-sen-se.gitbook.io/leetcode/401-600/563.-er-cha-shu-de-po-du.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
