【LeetCode】129. 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.
题解:
类似于 Path Sum 和 Path Sum II,迭代方法依然是后序遍历的思路,先遍历左右孩子。
Solution 1
 class Solution {
 public:
     int sumNumbers(TreeNode* root) {
         if (!root)
             return ;
         return dfs(root, );
     }
     int dfs(TreeNode* root, int sum) {
         if (!root)
             return ;
         sum = sum *  + root->val;
         if (!root->left && !root->right)
             return sum;
         return dfs(root->left, sum) + dfs(root->right, sum);
     }
 };
Solution 2
 class Solution {
 public:
     int sumNumbers(TreeNode* root) {
         if (!root)
             return ;
         queue<TreeNode*> q1;
         queue<int> q2;
         q1.push(root);
         q2.push(root->val);
         int sum = , cursum = ;
         TreeNode* cur = root;
         while (!q1.empty()) {
             cur = q1.front();
             cursum = q2.front();
             q1.pop();
             q2.pop();
             if (!cur->left && !cur->right) {
                 sum += cursum;
             }
             if (cur->left) {
                 q1.push(cur->left);
                 q2.push(cursum *  + cur->left->val);
             }
             if (cur->right) {
                 q1.push(cur->right);
                 q2.push(cursum *  + cur->right->val);
             }
         }
         return sum;
     }
 };
Solution 3
 class Solution {
 public:
     int sumNumbers(TreeNode* root) {
         if (!root)
             return ;
         stack<TreeNode*> s1;
         stack<int> s2;
         int sum = , cursum = ;
         TreeNode* cur = root, *pre = nullptr;
         while (cur || !s1.empty()) {
             while (cur) {
                 s1.push(cur);
                 cursum = cursum *  + cur->val;
                 s2.push(cursum);
                 cur = cur->left;
             }
             cur = s1.top();
             if (!cur->left && !cur->right) {
                 sum += cursum;
             }
             if (cur->right && cur->right != pre) {
                 cur = cur->right;
             } else {
                 pre = cur;
                 s1.pop();
                 cursum = s2.top();
                 s2.pop();
                 cursum -= cur->val;
                 cursum /= ;
                 cur = nullptr;
             }
         }
         return sum;
     }
 };
【LeetCode】129. Sum Root to Leaf Numbers的更多相关文章
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
		[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ... 
- 【LeetCode】129. Sum Root to Leaf Numbers (2 solutions)
		Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ... 
- LeetCode OJ 129. Sum Root to Leaf Numbers
		题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ... 
- LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II
		1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ... 
- [LeetCode] 129. Sum Root to Leaf Numbers 求根到叶节点数字之和
		Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ... 
- leetcode@ [129] Sum Root to Leaf Numbers (DFS)
		https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ... 
- leetcode 129. Sum Root to Leaf Numbers ----- java
		Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ... 
- [LeetCode] 129. Sum Root to Leaf Numbers 解题思路
		Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ... 
- Java for LeetCode 129 Sum Root to Leaf Numbers
		Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ... 
随机推荐
- 理解 $nextTick 的作用
			有同学在看 Vue 官方文档时,对 API 文档中的 Vue.nextTick 和 vm.$nextTick 的作用不太理解. 其实如果看一下深入响应式原理 - vue.js中的有关内容,可能会有所理 ... 
- java jvm内存管理/gc策略/参数设置
			1. JVM内存管理:深入垃圾收集器与内存分配策略 http://www.iteye.com/topic/802638 Java与C++之间有一堵由内存动态分配和垃圾收集技术所围成的高墙,墙外面的人想 ... 
- 前端开发日记之jQuery
			优先引入CDN上的jQuery,如果失效再引入本地的jQuery! <script src="http://libs.baidu.com/jquery/1.11.1/jquery.js ... 
- http 和 soap 关系 - 转载
			http soap关系 HTTP http:是一个客户端和服务器端请求和应答的标准(TCP).http协议其目的是为了提供一种发布和接收htttp页面的方法 一http协议的客户端与服务器的交互:由H ... 
- Mybatis层次结构图
- ceph存储 ceph Bluestore的架构
			ceph 目前是开源社区比较流行的分布式块存储系统,其以良好的架构,稳定性和完善的数据服务功能,获得的了广泛的部署和应用. 目前ceph 最大的问题是其性能相对较差,特别是无法发挥SSD等高速设备的硬 ... 
- 使用springmvc时报错org.springframework.beans.NullValueInNestedPathException: Invalid property 'department' of bean class [com.atguigu.springmvc.crud.entities.Employee]:
			使用springmvc时报错 org.springframework.beans.NullValueInNestedPathException: Invalid property 'departmen ... 
- HYSBZ - 2301 莫比乌斯反演
			链接 题解:直接用公式算,用容斥来减掉重复计算的部分 但是我犯了一个非常sb的错误,直接把abcd除k了,这样算a-1的时候就错了,然后举的例子刚好还没问题= = ,结果wa了好几发 //#pragm ... 
- 基于Spring MVC实现基于form表单上传Excel文件,批量导入数据
			在pom.xml中引入: <!--处理2003 excel--> <dependency> <groupId>org.apache.poi</groupId& ... 
- Oracle DBLink连接数过多的问题(Ora-02020)
			前不久开发人员编译存储时报ORA -02020 错,如下是解决方案步骤. 报错全信息: Error:OR A -04052在查: 找远程对象 NIP.PB_PERADDRESSLIST@DB_NI ... 
