leetcode — path-sum-ii

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Source : https://oj.leetcode.com/problems/path-sum-ii/
 *
 *
 * Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
 *
 * For example:
 * Given the below binary tree and sum = 22,
 *
 *               5
 *              / \
 *             4   8
 *            /   / \
 *           11  13  4
 *          /  \    / \
 *         7    2  5   1
 *
 * return
 *
 * [
 *    [5,4,11,2],
 *    [5,8,4,5]
 * ]
 *
 */
public class PathSum2 {


    /**
     *
     * 求出根节点到叶子节点的和等于sum的所有路径
     *
     * @param root
     * @param target
     * @return
     */
    public List<List<TreeNode>> pathSum (TreeNode root, int target) {
        List<List<TreeNode>> result = new ArrayList<List<TreeNode>>();
        List<TreeNode> path = new ArrayList<TreeNode>();
        pathSum(root, result, path, 0, target);
        return result;
    }

    public void pathSum (TreeNode root, List<List<TreeNode>> result, List<TreeNode> path, int sum, int target) {
        if (root == null) {
            if (sum == target) {
                List<TreeNode> list = new ArrayList<TreeNode>(path);
                result.add(list);
            }
            return ;
        }
        path.add(root);
        if (root.leftChild != null || root.rightChild != null) {
            // 如果左右子节点都为空只计算一个
            pathSum(root.leftChild, result, path, sum + root.value, target);
        }


        pathSum(root.rightChild, result, path, sum + root.value, target);
        path.remove(path.size()-1);

    }



    public TreeNode createTree (char[] treeArr) {
        TreeNode[] tree = new TreeNode[treeArr.length];
        for (int i = 0; i < treeArr.length; i++) {
            if (treeArr[i] == '#') {
                tree[i] = null;
                continue;
            }
            tree[i] = new TreeNode(treeArr[i]-'0');
        }
        int pos = 0;
        for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
            if (tree[i] != null) {
                tree[i].leftChild = tree[++pos];
                if (pos < treeArr.length-1) {
                    tree[i].rightChild = tree[++pos];
                }
            }
        }
        return tree[0];
    }

    private static void print (List<List<TreeNode>> list) {
        for (List<TreeNode> nodes: list) {
            System.out.println(Arrays.toString(nodes.toArray(new TreeNode[nodes.size()])));
        }
        System.out.println();
    }


    private class TreeNode {
        TreeNode leftChild;
        TreeNode rightChild;
        int value;

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

        public TreeNode() {

        }

        @Override
        public String toString() {
            return value + "";
        }
    }

    public static void main(String[] args) {
        PathSum2 pathSum2 = new PathSum2();
        char[] arr0 = new char[]{'5','4','8','1','#','3','3','7','2','#','#','5','1'};

        print(pathSum2.pathSum(pathSum2.createTree(arr0), 12));
        print(pathSum2.pathSum(pathSum2.createTree(arr0), 17));
    }
}

转载于:https://www.cnblogs.com/sunshine-2015/p/7823322.html

Unity3D实现项目限制功能(使用次数限制和时间限制) 大家好,我是心疼你的一切,不定时更新Unity开发技巧,觉得有用记得一键三连哦。在软件开发的时候,可能会遇到程序的使用次数限制,以及时间的限制,下面就写一下这两种方法是新建注册表,增加键值对,修改键值,完成对程序的控制提示:以下是本篇文章正文内容,下面案例可供参考以上就是讲了两种限制方法不定时更新Unity开发技巧,觉得有用记得一键三连哦。 阅读详情

相关推荐

qt使用qxlsx实现xlsx、xls表格文件快速写入和读取

新年第二篇文章,收尾一个去年计划中发表文章和实现功能。

kbq的博客 1万+

算法系列——路径总和II(Path Sum II)

题目描述 Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. For example: Given the below binary tree and sum = 22, 5

BridgeGeorge 660

MOS管的安全工作区SOA详解(一)限制线介绍

一篇文章让你搞懂MOS管的SOA曲线!

qq_45508321的博客 1万+

LeetCode112. 路径总和

本文为LeetCode112. 路径总和解析,欢迎学习!

Toby的博客 228

FortiOS路径遍历漏洞CVE-2022-41328实战复现:从环境搭建到漏洞利用全记录

本文深入解析FortiOS路径遍历漏洞CVE-2022-41328,从漏洞背景、影响范围到技术原理进行全面剖析。提供详细的漏洞复现环境搭建步骤和实战利用方法,并给出官方修复方案与临时缓解措施,帮助企业有效防御此类防火墙漏洞风险。

weixin_30485379的博客 530

实现AI Agent的多Agent协作能力

在当今复杂的计算环境和应用场景中,单个AI Agent的能力往往受到限制。实现AI Agent的多Agent协作能力的目的在于通过多个Agent之间的协同工作,充分发挥各自的优势,提高整体系统的性能和效率,以解决更为复杂的问题。本文章的范围涵盖了多Agent协作的核心概念、算法原理、数学模型、项目实战以及实际应用场景等方面,旨在为读者提供全面且深入的技术指导。本文将按照以下结构进行阐述:首先介绍多Agent协作的背景知识,包括目的范围、预期读者和文档结构等;

AI Python 编程之道的博客 1490

Path Sum II问题及解法

问题描述: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. 示例: Given the below binary tree and sum = 22, 5 / \

刘天的博客 366

【从零开始学习 SystemVerilog】1.1、SystemVerilog 概述

【从零开始学习 SystemVerilog】1.1、SystemVerilog 概述

努力,加油每一天鸭~ 1045

112.path-sum

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum ,判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。 叶子节点 是指没有子节点的节点。 示例 1: 输入:root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 输出:true 示例 2: 输入:root = [1,2,3], targetSum = 5 输出:false 示例 3: 输入:root =

qq_26000415的博客 722

OpenClaw 钉钉和飞书集成方案研究 & OpenClaw 研究分析报告

OpenClaw(原名 Clawdbot,曾用名 Moltbot)是一款开源的个人 AI 智能体,采用"本地优先"设计理念。与传统聊天机器人不同,OpenClaw 具备真正的系统级执行能力,能够实际操作用户的计算机系统,完成各种实际任务。核心优势本地部署:所有数据存储在用户本地设备,隐私零泄露系统级执行:具备完整的系统访问权限,可执行文件操作、命令运行等全渠道交互:支持多种即时通讯平台作为交互界面开源透明:MIT 许可证,代码完全开放,社区驱动开发应用场景个人助理:日程管理、任务提醒、邮件处理。

AI天才研究院 2348

如何给数据添加高斯白噪声?

首先测试集应该是加噪声的,至于训练集需不需要加噪声就见仁见智了。我个人测试的结果是,训练集如果不加噪声的话效果特别差,如果加噪声的话效果就比较好接受,大家可以自己试试看。在轴承故障研究中免不了要涉及到给数据主动加入噪声的情况,一般我们加的是高斯白噪声。由于热噪声是由大量自由电子的运动产生的,其统计特性服从高斯分布,故常将热噪声称为高斯白噪声。的频谱是均匀分布的,好像白光的频谱在可见光的频谱范围内均匀分布那样,所以热噪声又常称为。定义一:如果一个噪声,它的瞬时值服从。又是均匀分布的,则称它为高斯白噪声。

weixin_50642818的博客 5223

leetcode 113 pathSum2

地址:https://leetcode.com/problems/path-sum-ii/description//** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(in

qq_32417133的博客 279

Arcgis中的模型构建器(ModelBuilder)技术之批量图层转KML

这段时间全省各地都在开展耕地流出图斑整改恢复工作,期间或多或少都需要村委会协助摸排举证,除了举证软件外,奥维、外业精灵等软件想必大家接触得也很多。这就经常需要我们转kmz文件给业主或者摸排人员,那么有没有什么方法在arcgis下快速批量转kmz文件呢,今天就介绍一下如何在模型构建器(ModelBuilder)构建批量图层转KML的模型。

weixin_44671081的博客 1329

Path Sum II -- LeetCode

Path Sum II -- LeetCode

weixin_43418664的博客 134

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Leetcode 113. Path Sum II

Leetcode 113. Path Sum IIsource url题目描述Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.For example: Given the below binary tree and sum = 22,

vincent_cheung 331

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

【LeetCode with Python】 Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4

XNERV SURVEYS 2053

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

[LeetCode]113.Path Sum II

【题目】 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5

SmartSi 2044

Leetcode 113 Path Sum II(递归)

题目连接:Leetcode 113 Path Sum II解题思路:与 Leetcode 112 类似,搜索过程中记录路径并且在ans中保存可行方案。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right;...

不慌不忙、不急不躁 290

LeetCode | Unique Paths & II & Minimum Path Sum

62. Unique Paths 63. Unique Paths II 64. Minimum Path Sum

拖延癌患者自愈小记 392

Leetcode 113. Path Sum II 路径和2 解题报告

1 解题思想这道题和Leetcode 112是同宗的,112只要求输出是否存在一个Case,而这题要求输出所有Case的路径。 方法基本是一样他,遍历到底的时候保存一下路径就好Leetcode 112. Path Sum 路径和 解题报告 2 原题Given a binary tree and a sum, find all root-to-leaf paths where each path's

MebiuW的专栏 2189

LeetCode113. Path Sum II

LeetCode113. Path Sum II

bookman的博客 337

【leetcode刷题】62 63 64 unique path & unique path II & minimum path sum

原题链接 https://leetcode.com/problems/unique-paths/ https://leetcode.com/problems/unique-paths-ii/ https://leetcode.com/problems/minimum-path-sum/ 解题思路 这三道题比较类似,都是利用动态规划。 其中,unique path只需要把走到每一个格子里的方法数目加...

Arwen的博客 250

【LeetCode】113. Path Sum II 解题报告(Python)

【LeetCode】113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/path-sum-ii/descript...

负雪明烛 2166

LeetCode Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \

Coding for Dreams 2359

LeetCode - 113. Path Sum II

题目 LeetCode - 113. Path Sum II 题目链接 https://leetcode.com/problems/path-sum-ii/ 参考博客 解题思路 带路径的深度优先遍历。 解题源码 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode...

virgilshi的博客 158

LeetCode 113. Path Sum II

LeetCode 113. Path Sum IIDescription: Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. Example: For example: Given the below binary tree and

My Blog 196
上一篇: Script:利用外部表实现SQL查询Oracle告警日志Alert.log
下一篇: impala1.2.3 udf问题
weixin_34005042
博客等级 码龄11年 7095粉丝 1207原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值