129. Sum Root to Leaf Numbers
2016-07-30 15:02
225 查看
题目:根到叶子节点数字之和
Given a binary tree containing digits from
a number.
An example is the root-to-leaf path
Find the total sum of all root-to-leaf numbers.
For example,
The root-to-leaf path
The root-to-leaf path
Return the sum = 12 + 13 =
题意:
给定一个节点只包含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;
}
}
Given a binary tree containing digits from
0-9only, each root-to-leaf path could represent
a number.
An example is the root-to-leaf path
1->2->3which 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->2represents the number
12.
The root-to-leaf path
1->3represents 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;
}
}
相关文章推荐
- The Linux Kernel: Android?
- WebView 异常
- 爱TA就为TA掌厨一份蚝烙
- 使用wampserver搭建Apache+PHP+MySQL
- 单片机中断原理
- 安卓仿BOSS直聘的那种选择国家、地区和地区代码的列表
- Mob短信验证的具体使用
- 7.30笔记
- POJ 2352 Stars(HDU 1541 Stars)
- 彻底搞定C指针---指向指针的指针
- iOS常见加密算法原理
- Android 中应用的字体大小不受系统字体大小影响
- 安装Mongodb
- 730工作总结
- 常见的sql查询语句
- 三角形输出
- LightOJ 1422 Halloween Costumes 区间DP
- MVC
- linux基础命令2
- 算法:顺序查找