[LeetCode]Sum Root to Leaf Numbers
发布日期:2021-11-22 02:49:01
浏览次数:2
分类:技术文章
本文共 784 字,大约阅读时间需要 2 分钟。
Given a binary tree containing digits from 0-9
only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3
which represents the number 123
.
Find the total sum of all root-to-leaf numbers.
For example,
1 / \ 2 3
The root-to-leaf path 1->2
represents the number 12
.
The root-to-leaf path 1->3
represents the number 13
.
Return the sum = 12 + 13 = 25
.
public class Solution { public int sumNumbers(TreeNode root) { if(root == null) return 0; return sumNumber(root,"0"); } private int sumNumber(TreeNode root, String s) { if(root.left ==null && root.right ==null){ return Integer.valueOf(s+root.val); } return (root.left != null ?sumNumber(root.left, s+root.val):0)+(root.right != null?sumNumber(root.right, s+root.val):0); }}参考:
转载地址:https://blog.csdn.net/zxdfc/article/details/48787523 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月17日 15时21分30秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
我的合伙人
2021-06-30
linux内核定时器使用
2021-06-30
dynmic_debug动态控制kernel下的日志输出
2021-06-30
截图命令
2021-06-30
使用class_attribute 生sys文件系统下生成调试文件,方便使用adb调试
2021-06-30
Android fb0 截屏实现
2021-06-30
ubuntu下 安装 adb
2021-06-30
shell 中的ifeq
2021-06-30
如何把应用程序app编译进android系统
2021-06-30
MTK8127 把系统的apk不编译进入system.img
2021-06-30
ubuntu 14.04中文显示乱码问题
2021-06-30
vim 插件cscope 使用
2021-06-30
vim 函数列表插件
2021-06-30
Android 广播接收
2021-06-30
MTK 升级USB问题
2021-06-30
MTK 8127平台使用busybox
2021-06-30
vim 设置支持鼠标
2021-06-30
中断处理的tasklet(小任务)机制-不过如此
2021-06-30