Niuke Net Brushing Questions-the path from the root node to the leaf node of the binary tree and the specified value

Problem Description

Given a binary tree and a value sum, find the path where the sum of the node values ​​from the root node to the leaf node is equal to sum.

Input description:
Input a binary tree, a sum and

Output description:
output all paths that meet the conditions

Example

Example 1

Input
gives the following binary tree, sum=22,
Insert picture description here

Output
[
[5,4,11,2],
[5,8,9]
]

Solutions

analysis

  1. The depth-first search traversal method is adopted, and the path traversed is recorded through the backtracking method to determine whether each path meets the conditions.

Depth-first traversal: traverse from the follow node, extend a certain path until traversing to the leaf node and then return, and continue to traverse other paths.
Backtracking method: (Brief description: try every step and go back to the previous step if the condition is not met)
is a kind of optimization The search method, also known as the heuristic method, searches forward according to the optimal conditions to achieve the goal. But when you reach a certain step, you find that the original choice is not good or the goal is not achieved, so you go back one step and choose again. This technique of going back and going again if you can’t get through is the retrospective method.

method

  1. The depth-first search traversal method is adopted, and the path traversed is recorded through the backtracking method to determine whether each path meets the conditions.

Recursive analysis
Recursive parameters:
result: store all results; path: store the currently accessed path; root: binary tree; sum: path and
termination conditions:
1. Reach the leaf node, not satisfy sum
2. Reach the leaf node, and the path sum is sum
Recursive content:
1. Add nodes to the path
2. Recursive left and right nodes
3. Backtracking point: remove the result of the previous step

Code

// 思路1
public class Solution {
    
      
    
    private void dfs (ArrayList<ArrayList<Integer>> result, ArrayList<Integer> path, TreeNode root, int sum) {
    
    
        if (root == null) {
    
    
            return;
        }

		// 到达叶子节点,路径和为sum
        if (root.left == null && root.right == null) {
    
    
            // 到达叶子节点,没有满足sum
            if (sum - root.val == 0) {
    
    
                path.add(root.val);
                result.add(new ArrayList<>(path));
                path.remove(path.size() - 1);
            }
            // 到达叶子节点,没有满足sum
            return;
        }
        // 路径中添加节点
        path.add(root.val);
        // 递归左节点和右节点
        dfs(result, path, root.left, sum - root.val);
        dfs(result, path, root.right, sum - root.val);
        // 回溯点
        path.remove(path.size() - 1);
    }
}

Time complexity analysis:
Depth-first search traversal will traverse all nodes, so the time complexity is O(n)

Space complexity analysis:
Because extra space path will be applied as the structure of the backtracking storage path, the longest path of path is n (that is, the tree is a linked list structure), so the space complexity is O(n)

If you want to test, you can go directly to the link of Niuke.com to do the test

The path from the root node of the binary tree to the leaf node and the specified value

Guess you like

Origin blog.csdn.net/qq_35398517/article/details/113517580