题解 | #二叉树根节点到叶子节点和为指定值的路径#

二叉树根节点到叶子节点和为指定值的路径

http://www.nowcoder.com/practice/840dd2dc4fbd4b2199cd48f2dadf930a

package org.example.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.awt.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class DFSTest {

    static ArrayList<Boolean> vis = new ArrayList<>();

    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(5);
        TreeNode node2 = new TreeNode(4);
        TreeNode node3 = new TreeNode(8);
        node1.left = node2;
        node1.right = node3;
        TreeNode node4 = new TreeNode(1);
        TreeNode node5 = new TreeNode(11);
        node2.left = node4;
        node2.right = node5;
        TreeNode node6 = new TreeNode(9);
        node3.right = node6;
        TreeNode node7 = new TreeNode(2);
        TreeNode node8 = new TreeNode(7);
        node5.left = node7;
        node5.right = node8;
        System.out.println(JSONObject.toJSONString(pathSum(node1, 22)));

    }

    public static class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

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


    public static ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
        // write code here
        ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        LinkedList<Integer> path = new LinkedList<>();
        path.add(root.val);
        getPath(root, ret, path, sum - root.val);
        return ret;
    }

    private static void getPath(TreeNode root, ArrayList<ArrayList<Integer>> ret, LinkedList<Integer> path, int sum) {
        if (root.left == null && root.right == null) {
            System.out.println(root.val);
            if (sum == 0) {
                ret.add(new ArrayList<>(path));
            }
            return;
        }
        if (root.left != null) {
            path.add(root.left.val);
            getPath(root.left, ret, path, sum - root.left.val);
            path.removeLast();
        }
        if (root.right != null) {
            path.add(root.right.val);
            getPath(root.right, ret, path, sum - root.right.val);
            path.removeLast();
        }
    }

    public static void dfs(TreeNode root, ArrayList<ArrayList<Integer>> ret, LinkedList<Integer> path, int target) {
        if (root == null) {
            return;
        }
        path.add(root.val);
        target -= root.val;
        if (root.left == null && root.right == null && target == 0) {
            ret.add(new ArrayList<>(path));
        }
        dfs(root.left, ret, path, target);
        dfs(root.right, ret, path, target);
        path.removeLast();
    }
}
算法 文章被收录于专栏

数据结构和算法

全部评论

相关推荐

2024-12-29 11:08
湖南工业大学 Java
程序员牛肉:简历没什么大问题了。 而且不要再换项目了。三月份就开暑期实习了,现在都一月份了。实在来不及重新开一下项目了。把一个项目写完或许很快,但是把一个项目搞懂吃透并不简单。所以不要换项目了,把你简历上面的两个项目好好挖一挖吧。 具体 体现在:你能不能流利的说出你的项目的每一个功能点代码实现?你能不能说出在这块除了A技术之外,还有其他技术能够实现嘛?如果有其他技术能够实现,那你这块为什么选择了你当前用的这个技术?
投递牛客等公司
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务