129. Sum Root to Leaf Numbers
来源:互联网 发布:淘宝减肥产品排行榜 编辑:程序博客网 时间:2024/06/05 00:20
题目:根到叶子节点数字之和
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
.
题意:
给定一个节点只包含0到9之间数字的二叉树,每一条根节点到叶子节点的路径代表一个数。
例如,一条根节点到叶子节点路径为1->2->3,则代表的数字为123。
找到所有根节点到叶子节点所组成的数字之和。
思路一:
这道求根到叶节点数字之和的题跟之前的求Path Sum 二叉树的路径和很类似,都是利用DFS递归来解,这道题由于不是单纯的把各个节点的数字相加,而是每到一个新的数字,要把原来的数字扩大10倍之后再相加。
代码:C++版:4ms
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int sumNumbers(TreeNode* root) { return dfs(root, 0); }private: int dfs(TreeNode *root, int sum) { if (root == nullptr) return 0; if (root->left == nullptr && root->right == nullptr) //判断是否是叶子节点 return sum*10 + root->val; return dfs(root->left, sum*10 + root->val) + dfs(root->right, sum*10 + root->val); }};
思路二:
非递归实现,借助栈实现对遍历过得节点进行存储,以方便节点寻找上一级节点,先找到最左边路径的数,之后寻找右节点,及其他节点。
代码:C++版:4ms
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int sumNumbers(TreeNode* root) { stack<TreeNode*> nodes; if (!root) return 0; int total = 0; int current = 0; TreeNode *last = nullptr; while (root || !nodes.empty()) { if (root) { nodes.push(root); current = current*10 + root->val; root = root->left; } else { root = nodes.top(); if (root->right && root->right != last) { root = root->right; } else { nodes.pop(); last = root; if (root->left == nullptr && root->right == nullptr) total += current; current /= 10; root = nullptr; } } } return total; }};
思路三:
非递归方法,借助栈实现,在对二叉树的遍历过程中,将每次遍历到的节骨所组成的数字存储到该节点的值中,遇到叶子可以直接累加即可,这样操作修改了二叉树的值,相当于修改了二叉树结构,不符合题意,但是如果只是为了找出结果,也可以达到目的。
代码:java版:2ms
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int sumNumbers(TreeNode root) { if (root == null) return 0; int sum = 0; TreeNode curr; Stack<TreeNode> ws = new Stack<TreeNode>(); ws.push(root); while (!ws.empty()) { curr = ws.pop(); if (curr.right != null) { curr.right.val = curr.val*10 + curr.right.val; ws.push(curr.right); } if (curr.left != null) { curr.left.val = curr.val*10 + curr.left.val; ws.push(curr.left); } if (curr.left == null && curr.right == null) //叶子节点 sum += curr.val; } return sum; }}
- [LeetCode]129.Sum Root to Leaf Numbers
- LeetCode 129. Sum Root to Leaf Numbers
- [leetcode] 129.Sum Root to Leaf Numbers
- 129. Sum Root to Leaf Numbers
- 【leetcode】129. Sum Root to Leaf Numbers
- [leetcode] 129. Sum Root to Leaf Numbers
- [LeetCode]129. Sum Root to Leaf Numbers
- 129. Sum Root to Leaf Numbers LeetCode
- [LeetCode]129. Sum Root to Leaf Numbers
- leetcode 129. Sum Root to Leaf Numbers
- 129. Sum Root to Leaf Numbers
- Leetcode 129. Sum Root to Leaf Numbers
- 129. Sum Root to Leaf Numbers
- LeetCode *** 129. Sum Root to Leaf Numbers
- LeetCode-129.Sum Root to Leaf Numbers
- 129. Sum Root to Leaf Numbers
- 129. Sum Root to Leaf Numbers
- 129. Sum Root to Leaf Numbers
- Yii2 使用十九 Object对象机制
- JSOI2010 Codevs5227 盛夏的果实(重庆一中高2018级信息学竞赛测验7) 解题报告
- POJ 2377 Bad Cowtractors 【最大生成树】
- Python爬虫入门三之URLError异常处理
- 2016 年你应该学习的语言和框架
- 129. Sum Root to Leaf Numbers
- Linux下IO多路转接技术之epoll
- 不要随意使用手形光标
- BMKCloudNearbySearchInfo getUrlString 报错
- 编程之法,交替字符串问题
- poj3322 Bloxorz I
- Settings.xml文件详解
- csdn博客如何添加代码
- 关于fragmentTabhost点击底部切换不了的问题