# 32. 路径总和

java

```java
//树定义类 
public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int x) {
        val = x;
    }

}


// 树工具类
public class TreeUtils {

    public static int k=0;

    public static int k2=0;

    /**
     * 根据string创建树：节点的值小于等于9
     *
     *
     * @param s 先序的序列，空使用#表示
     * @return
     */
    public static TreeNode stringToTree(String s){
        k=0;
        TreeNode root=null;
        root=createTree(root,s);
        return root;
    }

    private static TreeNode createTree(TreeNode node,String s){
        char ch=s.charAt(k++);
        if(ch=='#') return null;
        else{
            node=new TreeNode(ch-'0');
            node.left=createTree(node.left,s);
            node.right=createTree(node.right,s);
            return node;
        }
    }

    /**
     * 符号以空格分隔，节点上的值可以大于9
     * @param str
     * @return
     */
    public static TreeNode stringToTreeWith(String str,String separator){
        k2=0;
        TreeNode root=null;
        String[] arr=str.trim().split(separator);
        root=createTreeWith(root,arr);
        return root;
    }

    private static TreeNode createTreeWith(TreeNode node,String[] arr){
        String v=arr[k2++];
        if(v.equals("#")) return null;
        else{
            node=new TreeNode(Integer.valueOf(v));
            node.left=createTreeWith(node.left,arr);
            node.right=createTreeWith(node.right,arr);
            return node;
        }
    }

    /**
     * 先序遍历
     * @param root
     */
    public static void travser(TreeNode root){
        if(root!=null){
            System.out.print(root.val+" ");
            travser(root.left);
            travser(root.right);
        }
    }

    /**
     * 中序遍历
     * @param root
     */
    public static void travser2(TreeNode root){
        if(root!=null){
            travser2(root.left);
            System.out.print(root.val+" ");
            travser2(root.right);
        }
    }

    /**
     * 后序遍历
     * @param root
     */
    public static void travser3(TreeNode root){
        if(root!=null){
            travser3(root.left);
            travser3(root.right);
            System.out.print(root.val+" ");
        }
    }
}


/**
 * 112. 路径总和
 * 给定一个二叉树和一个目标和，判断该树中是否存在根节点到叶子节点的路径，这条路径上所有节点值相加等于目标和。
 *
 * 说明: 叶子节点是指没有子节点的节点。
 *
 * 示例:
 * 给定如下二叉树，以及目标和 sum = 22，
 *
 *               5
 *              / \
 *             4   8
 *            /   / \
 *           11  13  4
 *          /  \      \
 *         7    2      1
 * 返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。
 */
public class Solution112 {
    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) return false;
        return adjust(root, 0, sum);
    }

    public boolean adjust(TreeNode root, int s, int target) {
        if (root != null) {
            if (root.left == null && root.right == null) {
                s += root.val;
                if (target == s) {
                    return true;
                }
            }
            boolean b1 = adjust(root.left, root.val + s, target);
            boolean b2 = adjust(root.right, root.val + s, target);
            if (b1 || b2) return true;
        }

        return false;
    }

    public static void main(String[] args) {
        String s = "5,4,11,7,#,#,2,#,#,#,8,13,#,#,4,#,1,#,#";
        int sum = 22;
        TreeNode root = TreeUtils.stringToTreeWith(s, ",");
        boolean r = new Solution112().hasPathSum(root, sum);
        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/32-lu-jin-zong-he.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.
