博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
129. Sum Root to Leaf Numbers
阅读量:4499 次
发布时间:2019-06-08

本文共 826 字,大约阅读时间需要 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.

题目含义:根节点到叶子节点的元素依次从高到地位构成一个整数,求所有整数的和

1     private int sums(TreeNode root,int sum) { 2         if (root ==null) return 0; 3         sum = sum*10 + root.val; 4         if (root.left == null && root.right==null) return sum; 5         return  sums(root.left,sum) + sums(root.right,sum); 6     } 7      8     public int sumNumbers(TreeNode root) { 9         return sums(root,0);10     }

 

 

 

 

 

转载于:https://www.cnblogs.com/wzj4858/p/7711862.html

你可能感兴趣的文章
LeetCode(15)题解--3Sum
查看>>
华为离职副总裁徐家骏:年薪千万的工作感悟
查看>>
osx系统使用技巧 -- 虚拟机virtualbox
查看>>
java中的Runtime 和Process 类用法 以及开发中的单例模式 暑假十一天
查看>>
HashSet HashTable HashMap 区别
查看>>
[字符串]在一个字符串中查找第一次只出现一次的字符
查看>>
Arm Linux 系统如何实现java程序的运行
查看>>
【WPF】ListView自定义分页
查看>>
不能从const char *转换为LPCWSTR --VS经常碰到
查看>>
联想笔记本电脑Ubuntu系统下触摸板的锁定
查看>>
读Java 804 - Quick refresher
查看>>
求1~n整数中1出现的次数(《剑指offer》面试题43)
查看>>
转载 Android Map Api 使用和开发 定位我的位置、地图弹出泡泡、通过经纬度获取地址 浮动搜索框 ,通过地址名称获取经纬度和详细地址并定位...
查看>>
selenium server在页面加载超时浏览器与driver通信失败时的妙用
查看>>
CSS haslayout
查看>>
突破ewebeditor中无组件200K上传
查看>>
验证码和输入框对其
查看>>
应用多环境部署和Redis高可用
查看>>
最新地址
查看>>
# 20162319 2016-2017-2 《程序设计与数据结构》第4周学习总结
查看>>