二叉树所有路径

1. 二叉树的所有路径

深度优先遍历(DFS),使用临时变量(这里使用StringBuilder,也可以使用String,但是String是不可变对象,+= 操作将new出多个String对象,影响效率)保存当前节点的值,当节点的左右子树不都为null时,递归调用,当节点左右子树都为null时,即递归到叶子节点,将临时变量的值保存。LeetCode链接

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. 路径总和

参考例子 1. 二叉树的所有路径,我们可以通过DFS遍历得到二叉树的所有路径。使用flag记录是否找到对应的分支,使用变量total累加每个节点的val值,当遍历的叶子节点时,判断total是否与给定的sum相等,相等则将flag置为true,提前退出递归。LeetCode链接

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. 路径总和(2)

得到路径和等于给定值的所有路径。结合 1. 二叉树的所有路径2. 路径总和,我们可以通过dfs获取所有路径,通过临时变量计算出当前路径总和是否等于sum值。多了一个使用List变量保存节点当前值的操作。LeetCode链接

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));
    }
}

你可能感兴趣的:(算法)