Java - the path of a certain value in the binary tree (2)

topic link

Niuke.com online oj question - the path with a certain value in the binary tree (2)

topic description

Enter the root node root of a binary tree and an integer expectNumber, and find all paths whose sum of node values ​​in the binary tree is expectNumber.
1. The path of this question is defined as the node from the root node of the tree down to the leaf node.
2. The leaf node refers to the node without child nodes
. 3. The path can only go from the parent node to the child node, not From child node to parent node
4. The total number of nodes is n

If the root of the binary tree is {10,5,12,4,7}, and the expectNumber is 22
insert image description here
, the legal path is [[10,5,7],[10,12]]

Data range:
the total number of nodes in the tree is in the range [0, 5000]
-1000 <= node value <= 1000
-1000 <= expectNumber <= 1000

Topic example

Example 1

Input:
{10,5,12,4,7},22

Return value:
[[10,5,7],[10,12]]

Explanation:
Returning [[10,12],[10,5,7]] is also correct

Example 2

Input:
{10,5,12,4,7},15

return value:
[]

Example 3

Input:
{2,3},0

return value:
[]

Example 4

Input:
{1,3,4},7

return value:
[]

problem solving ideas

This question needs to be realized through deep search and backtracking, which is divided into the following steps

  1. Add the value of the node
  2. Determine whether the condition is met, and if so, add the current result to the result set
  3. continue deep search
  4. Judging whether it is out of bounds, if it is out of bounds, it will fall back to the previous state

Define an ArrayList<ArrayList<Integer>> result variable to store the final result
Define an ArrayList<Integer> list variable to store the process collection during one traversal

Implement the depth-first algorithm FindPathDFS(TreeNode root, int expectNumber, ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list), the parameters are: current recursive node, current expected number sum, result set result, process set list , no return value

First judge whether the root is empty, if it is empty, return directly
and then add the current root.val to the process set
to update the target value, expectNumber -= root.val;

If the current root has no left subtree and right subtree (currently a leaf node), and expectNumber == 0 (the sum of all numbers of the current branch is expectNumber), add list to result

Call the function recursively, passing in the left and right subtrees

Finally, if the current list does not meet the conditions, it returns directly to the previous state: the list deletes the last element, list.remove(list.size() - 1);

full 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 {
    
    
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int expectNumber) {
    
    
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        if(root == null){
    
    
            return result;
        }
        FindPathDFS(root, expectNumber, result, new ArrayList<>());
        return result;
    }

    private void FindPathDFS(TreeNode root, int expectNumber, ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list) {
    
    
        if(root == null){
    
    
            return;
        }
        list.add(root.val);
        expectNumber -= root.val;
        if(root.left == null && root.right == null && expectNumber == 0){
    
    
            result.add(new ArrayList<>(list));
        }
        FindPathDFS(root.left, expectNumber, result, list);
        FindPathDFS(root.right, expectNumber, result, list);
        list.remove(list.size() - 1);
    }
}

Guess you like

Origin blog.csdn.net/m0_60867520/article/details/130347649