当前位置: 首页 > news >正文

LeetCode -- Sum Root to Leaf Numbers

题目描述:


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.


思路:
DFS问题,即从根节点出发进入统计(存为string s),如果左节点不为空,进入左子树统计;如果右子树不为空进入右子树统计。如果是叶子节点,记录状态放入总结果(List<string> result)。
遍历完之后,再遍历一遍result转为整形累加计算结果即可。


实现代码:


/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int SumNumbers(TreeNode root) 
    {
        if(root == null){
    		return 0;
    	}
         var ret = new List<string>();
         Sum(root, "",ref ret);
         
         var s = 0;
         for(var i = 0;i < ret.Count; i++){
         	 s += int.Parse(ret[i]);
         }
         
         return s;
    }


private void Sum(TreeNode root, string s,ref List<string> result)
{
	if(root.left == null && root.right == null){
		result.Add(s + root.val);
		return;
	}
	
	if(root.left != null){
		Sum(root.left, s + root.val, ref result);
	}
	if(root.right != null){
		Sum(root.right, s + root.val, ref result);
	}
}
}


相关文章:

  • 移动设备管理(MDM)与OMA(OTA)DM协议向导(三)——AAA服务器
  • LeetCode -- Surrounded Regions
  • LeetCode -- Triangle
  • Nebula3中的骨骼动画: Animation子系统
  • LeetCode -- Ugly Number II
  • LeetCode -- Ugly Number
  • vim 显示行号、语法高亮、自动缩进的设置
  • LeetCode -- Linked List cycle
  • 根据textbox中的值,改变dropdownlist的选项
  • LeetCode -- Basic Calculator II
  • 完整SQL分页存储过程(支持多表联接)
  • LeetCode -- Bitwise AND of Numbers Range
  • C 符号列表
  • LeetCode -- Linked List Cycle II
  • LeetCode -- LRU Cache
  • [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示
  • express + mock 让前后台并行开发
  • JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
  • javascript面向对象之创建对象
  • js学习笔记
  • Twitter赢在开放,三年创造奇迹
  • ucore操作系统实验笔记 - 重新理解中断
  • 给新手的新浪微博 SDK 集成教程【一】
  • 关于Flux,Vuex,Redux的思考
  • 漫谈开发设计中的一些“原则”及“设计哲学”
  • 巧用 TypeScript (一)
  • 日剧·日综资源集合(建议收藏)
  • 学习ES6 变量的解构赋值
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • 在weex里面使用chart图表
  • 正则表达式小结
  • JavaScript 新语法详解:Class 的私有属性与私有方法 ...
  • Mac 上flink的安装与启动
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • !!Dom4j 学习笔记
  • !$boo在php中什么意思,php前戏
  • # Apache SeaTunnel 究竟是什么?
  • #ifdef 的技巧用法
  • ( 10 )MySQL中的外键
  • (4) PIVOT 和 UPIVOT 的使用
  • (C语言)共用体union的用法举例
  • (笔试题)合法字符串
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (二)Eureka服务搭建,服务注册,服务发现
  • (实战篇)如何缓存数据
  • (原創) 如何動態建立二維陣列(多維陣列)? (.NET) (C#)
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (转)GCC在C语言中内嵌汇编 asm __volatile__
  • (转)JAVA中的堆栈
  • (转)重识new
  • ****** 二十三 ******、软设笔记【数据库】-数据操作-常用关系操作、关系运算
  • .gitignore文件设置了忽略但不生效
  • .Net Attribute详解(上)-Attribute本质以及一个简单示例
  • .NET Core 2.1路线图
  • .NET NPOI导出Excel详解