# 56. 二叉树的坡度

java

```java
import leetcode.common.TreeNode;
import leetcode.common.TreeUtils;

/**
 * 563. 二叉树的坡度
 *
 * 给定一个二叉树，计算整个树的坡度。
 *
 * 一个树的节点的坡度定义即为，该节点左子树的结点之和和右子树结点之和的差的绝对值。空结点的的坡度是0。
 *
 * 整个树的坡度就是其所有节点的坡度之和。
 *
 * 示例:
 *
 * 输入:
 *          1
 *        /   \
 *       2     3
 * 输出: 1
 * 解释:
 * 结点的坡度 2 : 0
 * 结点的坡度 3 : 0
 * 结点的坡度 1 : |2-3| = 1
 * 树的坡度 : 0 + 0 + 1 = 1
 * 注意:
 *
 * 任何子树的结点的和不会超过32位整数的范围。
 * 坡度的值不会超过32位整数的范围。
 */
public class Solution563 {
    /**
     * 计算各个节点的坡度和
     * @param root
     * @return
     */
    public int findTilt(TreeNode root) {
        if(root==null) return 0;
        return findTilt(root.left)+findTilt(root.right)+Math.abs(todo(root.left)-todo(root.right));
    }

    /**
     * 计算以root为起始点的坡度值
     * @param root
     * @return
     */
    public int todo(TreeNode root) {
        if(root==null) return 0;
        if(root.left==null&&root.right==null){
            return root.val;
        }
        return root.val+todo(root.left)+todo(root.right);
    }

    public static void main(String[] args){
        TreeNode root=TreeUtils.stringToTree("124###35###");
        int r=new Solution563().findTilt(root);
        System.out.println(r);
    }
}
```


---

# 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://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/shu-ju-jie-gou-lei/56-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.
