Java implements a path that sums to a value in a binary tree

Input a binary tree and an integer, print out all paths in the binary tree whose sum of node values ​​is the input integer. From the root node of the tree down to the leaf node, all the nodes passed through form a path.

Preorder traversal is used, so that the root node can be visited first.

There are the following trees

                5

           3        7

       2        4

There are 5->3->4 and 5->7 if the sum of the paths is 12, but there will be 5->3->2 first. If it is not satisfied, it will fall back to 5->3 , so choose the stack to save path information

code

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

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

    public static void findAllPathes(TreeNode root, int expectedSum) {
        if (root == null) {
            return;
        }

        Stack<Integer> path = new Stack<>();
        findPath(root, expectedSum, 0, path);
    }

    private static void findPath(TreeNode root, int expectedSum, int sum, Stack<Integer> path) {
        // accumulate path values
        sum += root.val;
        // add the current node to the path
        path.push(root.val);
        // If the current node is a leaf node (that is, there is no left or right child node), and the current accumulated value is equal to the expected value, output path information
        if (root.left == null && root.right == null && sum == expectedSum) {
            for (Integer val : path) {
                System.out.print(val + " ");
            }
            System.out.println();
        }

        // If the left child node is not empty, indicating that it is not a leaf node, continue to find the path
        if (root.left != null) {
            findPath(root.left, expectedSum, sum, path);
        }
        // If the right child node is not empty, indicating that it is not a leaf node, continue to find the path
        if (root.right != null) {
            findPath(root.right, expectedSum, sum, path);
        }

        // If the current node does not meet the requirements, remove the current node from the path
        path.pop();
    }

    public static void main(String[] args) {
        //           5
        //       3       7
        //   2       4
        TreeNode root = buildTree();
        findAllPathes(root, 12);
    }

    /**
     * Create tree:
     *              5
     *          3       7
     *      2       4
     * @return
     */
    private static TreeNode buildTree(){
        TreeNode root = new TreeNode(5);
        TreeNode left = new TreeNode(3);
        TreeNode left1 = new TreeNode(2);
        TreeNode right1 = new TreeNode(4);
        left.left = left1;
        left.right = right1;
        TreeNode right = new TreeNode(7);
        root.left = left;
        root.right = right;
        return root;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325925702&siteId=291194637