Binary tree all paths

1. All paths of a binary tree

Depth-first traversal (DFS), using temporary variables (StringBuilder can also be used here, but String is an immutable object, += operation will create multiple String objects, affecting efficiency) to save the value of the current node, when the node When the left and right subtrees are not both null, call recursively. When the left and right subtrees of the node are both null, it recurses to the leaf node and saves the value of the temporary variable. LeetCode link

public class LeetCode257 {
    
    
    public static List<String> binaryTreePaths(TreeNode root) {
    
    
        List<String> res = new ArrayList<>();
        if (root == null) return res;
        helper(root, new StringBuilder(), res);
        return res;
    }

    private static void helper(TreeNode node, StringBuilder sb, List<String> res) {
    
    
        sb.append(node.val);
        if (node.left != null || node.right != null) {
    
    
            sb.append("->");
            if (node.left != null) {
    
    
                helper(node.left, new StringBuilder(sb), res);
            }
            if (node.right != null) {
    
    
                helper(node.right, new StringBuilder(sb), res);
            }
        }
        if (node.left == null && node.right == null)
            res.add(sb.toString());
    }
    
    public static void main(String[] args) {
    
    
        TreeNode node = new TreeNode(1);
        node.left = new TreeNode(2);
        node.left.right = new TreeNode(5);
        node.right = new TreeNode(3);
        System.out.println(binaryTreePaths(node));
    }
}

2. Path sum

Refer to the example 1. 二叉树的所有路径, we can get all the paths of the binary tree through DFS traversal. Use flag to record whether the corresponding branch is found, and use the variable total to accumulate the val value of each node. When traversing the leaf nodes, determine whether the total is equal to the given sum, and if it is equal, the flag is set to true, and the recursion is exited early. LeetCode link

public class LeetCode112 {
    
    
    private static boolean flag = false;

    public static boolean hasPathSum(TreeNode root, int sum) {
    
    
        if (root == null) return flag;
        helper(root, sum, 0);
        return flag;
    }

    private static void helper(TreeNode node, final int sum, int tmp) {
    
    
        if (node == null || flag) return;
        tmp += node.val;
        if (node.left != null)
            helper(node.left, sum, tmp);
        if (node.right != null)
            helper(node.right, sum, tmp);
        if (node.left == null && node.right == null && tmp == sum) {
    
    
            flag = true;
        }
    }

    public static void main(String[] args) {
    
    
        TreeNode node = new TreeNode(8);
        node.left = new TreeNode(9);
        node.right = new TreeNode(-6);
        node.right.left = new TreeNode(5);
        node.right.right = new TreeNode(9);
        System.out.println(hasPathSum(node, 7));
    }
}

3. Path sum (2)

Get the path and all paths equal to the given value. Binding 1. 二叉树的所有路径and 2. 路径总和we can obtain all paths through DFS, calculated by the temporary variable is equal to the sum of the sum current path value. One more operation is to use the List variable to save the current value of the node. LeetCode link

public class LeetCode113 {
    
    
    public static List<List<Integer>> pathSum(TreeNode root, int sum) {
    
    
        List<List<Integer>> res = new ArrayList<>();
        helper(root, sum, 0, res, new ArrayList<>());
        return res;
    }

    private static void helper(TreeNode node, int sum, int total, List<List<Integer>> res, List<Integer> tmpList) {
    
    
        if (node == null) return;
        total += node.val;
        tmpList.add(node.val);
        if (node.left != null)
            helper(node.left, sum, total, res, new ArrayList<>(tmpList));
        if (node.right != null)
            helper(node.right, sum, total, res, new ArrayList<>(tmpList));
        if (node.left == null && node.right==null && total == sum)
            res.add(tmpList);
    }
    
    public static void main(String[] args) {
    
    
        TreeNode node = new TreeNode(8);
        node.left = new TreeNode(1);
        node.right = new TreeNode(-3);
        node.right.left = new TreeNode(4);
        node.right.right = new TreeNode(3);
        System.out.println(pathSum(node, 9));
    }
}

Guess you like

Origin blog.csdn.net/Dkangel/article/details/107515864