Brush with a java prove safety offer (in the binary tree and the path of a certain value)

Title Description

Input nodes and a binary tree with an integer, the binary print values ​​of the nodes and the paths to all the input integers. Forming a path to the path definition begins from the root node of the tree down to the leaf node has been traversed nodes. (Note: the return value in the list, a large array Array front)

Cattle off network link

java code

import java.util.ArrayList;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    ArrayList<Integer> list = new ArrayList<>();
    ArrayList<ArrayList<Integer>> listAll = new ArrayList<>();
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        if (root == null) return listAll;
        list.add(root.val);
        target -= root.val;
        if (target == 0 && root.left == null && root.right == null) {
            listAll.add(new ArrayList<Integer>(list));
        }
        FindPath(root.left, target);
        FindPath(root.right, target);
        list.remove(list.size()-1);
        return listAll;
    }
}

Guess you like

Origin www.cnblogs.com/dpnlp/p/yongjava-shua-jian-zhioffer-er-cha-shu-zhong-he-we.html