LeetCode Binary Tree Paths

基于FPGA的DDS信号发生器 之前的博客讲到了DDS的基本原理,现在用Verilog代码实现DDS,能够产生四种波形,方波,三角波,正弦波,锯齿波,用按键来控制频率和选择波形。其中按键消抖模块来自小梅哥的FPGA自学笔记设计与验证这本书。 module DDS_Module( Clk, Rst_n, EN, Fword, Key1, Pword, DA_Clk, DA_Data ); input Clk... 阅读详情

原题链接在这里:https://leetcode.com/problems/binary-tree-paths/

DFS 依次添加,终止条件是叶子节点。

Note: 1. 如果使用StringBuilder, 在递归调用时,必须写成new StringBuilder(sb). 否则会报错.

e.g. [1,2,3], 会写成["1->2","1->23"]. 因为扫过点2,StringBuilder 会变成1->2, 扫过点3,原来的点2没有去掉,就会变成1->23,但是第一个结果没有变成1->23是因为,用到sb.toString()时自动做了copy by value.

2 . 但如果使用String,在递归调用时可以直接使用str, 因为String 和 int, float一样是priority type, 是copy by value,而不像array等object式copy by reference.

3. 在使用String时,前面要写String str = new String(), 等号右面的初始化不可以省略。

AC  Java:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<String>();
        if(root == null){
            return res;
        }
        dfs(root, new StringBuilder(), res);
        return res;
    }
    private void dfs(TreeNode root, StringBuilder sb, List<String> res){
        
        if(root.left == null && root.right == null){
            sb.append(root.val);
            res.add(sb.toString());
            return;
        }
        sb.append(root.val);
        sb.append("->");
        if(root.left != null){
            dfs(root.left,new StringBuilder(sb),res);
        }
        if(root.right != null){
            dfs(root.right,new StringBuilder(sb),res);
        }
    }
}
上面使用StringBuilder, 下面使用String.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<String>();
        if(root == null){
            return res;
        }
        String str = new String();
        dfs(root, str, res);
        return res;
    }
    private void dfs(TreeNode root, String str, List<String> res){
        
        if(root.left == null && root.right == null){
            str = str + String.valueOf(root.val);
            res.add(str);
            return;
        }
        str += String.valueOf(root.val);
        str += "->";
        if(root.left != null){
            dfs(root.left,str,res);
        }
        if(root.right != null){
            dfs(root.right,str,res);
        }
    }
}


Doris StreamLoad任务报错Connection reset 背景 Spark程序扫一张Hive表(大小3-7G),然后用http协议的streamLoad任务提交给Doris集群,在Doris集群从0.13.15升级到0.14.12后spark程序报错streamLoad时突然会Connection reset 分析 enable_http_server_v2 这个参数可以参考FE中文配置中查看到,作用是对于Doris界面是否开启新风格的界面,但是实际上他的作用远不止如此,请接着看 新老版本对于enable_http_server_v2参数默认值是不同的 在0.13 阅读详情

相关推荐

Windows: 深入剖析pip install SSLError与SSL模块缺失的根源及系统级修复

本文深入分析Windows系统下pip install SSLError与SSL模块缺失的根本原因,提供系统级修复方案。从Python安装包缺失SSL支持、OpenSSL库冲突到环境变量配置问题,详细讲解四大成因及对应的解决方案,包括重新安装Python、手动修复OpenSSL依赖等实用技巧,帮助开发者彻底解决HTTPS连接问题。

weixin_28595749的博客 459

LeetCode257 BinaryTreePaths(打印根节点到叶子节点的左右路径) Java题解

题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"]

u012249528的专栏 1965

一文读懂Cache一致性原理

本文介绍了为什么需要维护cache一致性、一致性协议的本质和类别,以及如何设计一致性协议。

W1Z1Q的专栏 6833

LeetCode 257. Binary Tree Paths 二叉树的所有路径(Java)

题目: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. 解答: 采用深度优先遍历的思想,通过递归调用,逐步搜索叶子节点, 用 list 记录最后的结果,用一个字符串 path 来记录当前路径 如果是叶子节点,就把这个路径保存到 list 中 如果不是叶子节点...

qq_41878358的博客 229

MAC M1用户必看:UTM虚拟机安装Win11全流程避坑指南(附驱动下载)

本文为MAC M1用户提供UTM虚拟机安装Win11的完整指南,涵盖镜像准备、驱动优化及性能调优等关键步骤。特别针对TPM 2.0验证、网络连接等常见问题提供解决方案,帮助用户高效运行Windows 11 ARM版。

weixin_29204205的博客 337

双轴按键摇杆模块原理与嵌入式驱动实战

双轴按键摇杆是一种融合模拟量(X/Y轴)与数字量(Z轴按键)的复合型人机交互传感器,其本质基于电位器分压原理与机械开关通断特性。通过ADC采集连续电压信号并结合数字IO检测按键状态,可实现高鲁棒性的二维运动感知与触发控制。该方案技术门槛低、成本可控、兼容性强,广泛应用于智能小车、DIY游戏手柄、工业HMI及教育开发板等嵌入式场景。文中深入解析电位器线性度、KEY引脚开漏特性、ADC参考电压稳定性及软件消抖状态机等关键工程要素,为基于Arduino、STM32或ESP32平台的快速集成提供可复用的技术路径。

weixin_30995917的博客 82

leetcode-java-257. Binary Tree Paths

/* 思路: 1.采用递归 左子树递归找到路径(根节点+左子树) 右子树递归找到路径(根节点+右子树) 2.递归停止条件: 递归到叶子节点时终止,即root.left==null && root.right == null时,将字符串add到result中。 */ public class Solution { public List<String> binaryTreePaths(T

王欢的博客 917

WPF的基础设施:XAML基础语法

XAML是WPF的核心标记语言,采用XML语法实现界面与逻辑分离。其基础语法包括:1)控件声明方式(特性语法和属性元素语法);2)x:Name与Name的区别(适用范围和编译行为);3)注释规范与代码折叠技巧;4)实用特性如属性继承、附加属性等。文章还提供了常见错误排查方法,建议通过实践练习掌握XAML开发技巧。全文系统介绍了XAML的核心语法与应用场景,是WPF开发者的基础学习指南。

Hare的博客 1755

LeetCode | 0257. Binary Tree Paths二叉树的所有路径【Python】

LeetCode 0257. Binary Tree Paths二叉树的所有路径【Easy】【Python】【DFS】 Problem LeetCode Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example: Input: 1 / \...

Wonz 587

使用mindie:2.0.RC2-800I-A2-py311-openeuler24.03-lts制作一个通用的模型推理性能测试的镜像

本文详细介绍了在NPU环境下进行模型推理性能测试的完整流程。首先配置了测试所需的环境和设备信息,包括Python 3.10.9和3.11.6版本、测试脚本和镜像文件。然后分步骤说明了镜像制作过程:创建特权容器、安装Python 3.10.9、配置VLLM环境,并详细描述了测试容器的启动方法。最后提供了镜像打包和使用指南,包括如何加载镜像、启动容器、运行测试脚本(以DeepSeek-32B模型为例)以及更换测试模型的步骤。整个过程涉及环境变量配置、权限设置和服务启动等关键操作。

m0_50105717的博客 1015

学Simulink——基于Simulink的固态变压器(SST)多级协同控制​

本文介绍了基于Simulink的固态变压器(SST)多级协同控制系统设计与仿真方法。SST作为智能电网关键设备,包含AC-DC整流、DC-DC隔离和DC-AC逆变三级变换器。针对级间耦合问题,提出了能量-功率协同控制策略,通过纵向解耦和横向联动实现稳定运行。详细演示了在Simulink中搭建三级主电路、设计协同控制器(含电网定向控制、功率前馈和虚拟惯性控制)的全过程,并通过负载突变测试验证了系统动态性能(直流母线电压波动<2%)。最后给出了工程部署建议,包括数字控制延时补偿、启动序列设计和代码自动生成

xiaoheshang_123的博客 203

leetcode 257. Binary Tree Paths

/* leetcode 257. Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are:["1->2->5", "1->3"]题

编码之路 398

华为面试手撕真题【求1-n的最小公倍数】

求1-n的最小公倍数

MISAYAONE的博客 2万+

leetcode 257 Binary Tree Paths 详细解答

leetcode 257 Binary Tree Paths 详细解答 题目比较简单,用迭代即可

qq_22210659的博客 160

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

数学建模模型总结

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

【LeetCode】257.Binary Tree Paths(Easy)解题报告

【LeetCode】257.Binary Tree Paths(Easy)解题报告题目地址:https://leetcode.com/problems/binary-tree-paths/description/ 题目描述:   Given a binary tree, return all root-to-leaf paths.   For example, given the f

郝春雨的博客 257

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

Leetcode学习笔记:#257. Binary Tree Paths

Leetcode学习笔记:#257. Binary Tree Paths. Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. 实现: public List<String> binaryTreePaths(TreeNode root){ List&l...

ccystewart的博客 202

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

leetcode 257: Binary Tree Paths

Binary Tree Paths Total Accepted: 3755 Total Submissions: 17536 Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3

西施豆腐渣 4438

LeetCode: 257. Binary Tree Paths

LeetCode: 257. Binary Tree Paths 题目描述 Given a binary tree, return all root-to-leaf paths. **Note: **A leaf is a node with no children. Example: Input: 1 / \ 2 3 \ 5 Output: ["1-&gt;2-&g...

杨领well的专栏 236

[leetcode][tree] Binary Tree Paths

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"] /**

Clairezz_的博客 1075

[LeetCode257] Binary Tree Paths

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"] /**

coder 进阶的专栏 2756

leetcode Binary Tree Paths

题目连接 https://leetcode.com/problems/binary-tree-paths/ Binary Tree Paths Description Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1/ ...

dong12276的博客 149

LeetCode题解-257-Binary Tree Paths

原题 原题链接:https://leetcode.com/problems/binary-tree-paths/ 解法概览 解法1:迭代法,后序遍历 解法2:递归法,参考了https://discuss.leetcode.com/topic/23047/clean-java-solution-accepted-without-any-helper-recursive-fun

WangT443的博客 900

[Leetcode]Binary Tree Paths

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"] algorithm: dfs  /*

dyllanzhou的专栏 391

【leetcode】257. Binary Tree Paths

problem 257. Binary Tree Paths solution1:recursive递归方法。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * ...

weixin_30737433的博客 181

LeetCode257——Binary Tree Paths

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 实现:

代码菌的blog 4499

[Leetcode] Binary Tree Paths

二叉树路径查找,利用递归的方法查找,我的方法比较慢,因为需要先遍历到叶子节点再返回路径,如果在遍历的同时生成路径就会快很多 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * Tree

daniren的博客 324
上一篇: Java Convert String & Int
下一篇: LeetCode Path Sum II
Dylan_Java_NYC
博客等级 码龄11年 1粉丝 134原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值