Leetcode[129]-Sum Root to Leaf Numbers
2015-06-12 14:15
579 查看
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,
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.
这道题让我真的醉了,先说下思路吧。
先求出每个叶子节点的值,然后将这些值相加。求叶子节点的值可以通过深度优先遍历,一遍往下,一遍增加各个节点的值,如果是叶子节点就将数组值的和加入到一个只有叶子节点的数组中,最后求这个只有叶子节点的数组的值得和。从头到尾自己写的代码,可以AC了,还没有进行优化,先做到这里吧。
代码(C++):
附加一个求深度的代码:
方法二:(递归法)
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.
这道题让我真的醉了,先说下思路吧。
先求出每个叶子节点的值,然后将这些值相加。求叶子节点的值可以通过深度优先遍历,一遍往下,一遍增加各个节点的值,如果是叶子节点就将数组值的和加入到一个只有叶子节点的数组中,最后求这个只有叶子节点的数组的值得和。从头到尾自己写的代码,可以AC了,还没有进行优化,先做到这里吧。
代码(C++):
/** * 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: //return sum of newleafValue int sumNumbers(TreeNode* root) { if(root == NULL) return 0; if(!root->left && !root->right) return root->val; vector<int> nums = saveLeafNums(root); return sumV(nums); } //save leftnode to vector vector<int> saveLeafNums(TreeNode * root){ vector<int> nums,nodeValue; int eachSum = 0; stack<TreeNode *> stk; map<TreeNode *, int> visited; TreeNode *p = root; stk.push(p); visited[p] = 0; nodeValue.push_back(p->val); while(!stk.empty()){ while(p->left && visited[p]==0 ){ //if this node is visited,break if(visited[p->left]==1) break; p = p->left; stk.push(p); visited[p] = 0; tenMutilVector(nodeValue);//enlarge vector element's value nodeValue.push_back(p->val); } if(!stk.empty()) { p = stk.top(); if(!p->left && !p->right){ nums.push_back(sumV(nodeValue)); } if(p->right && (visited.find(p->right)==visited.end() || visited[p->right]==0)){ p = p->right; stk.push(p); visited[p] = 0; tenMutilVector(nodeValue); nodeValue.push_back(p->val); continue; } visited[p] = 1; stk.pop(); tenDivideVector(nodeValue); nodeValue.resize(nodeValue.size()-1); } } return nums; } void tenMutilVector(vector<int> &nums) { int n = nums.size(); for(int i = 0; i < n; i++){ nums[i] *= 10; } } void tenDivideVector(vector<int> &nums) { int n = nums.size(); for(int i = 0; i < n; i++){ nums[i] /= 10; } } int sumV(vector<int> &nums){ int n = nums.size(); int sumValue = 0; for(int i = 0; i < n; i++){ sumValue += nums[i]; } return sumValue; } };
附加一个求深度的代码:
int getDepth(TreeNode* root) { if(root == NULL) return 0; int ldepth = 0,rdepth = 0; ldepth = getDepth(root->left); rdepth = getDepth(root->right); return ldepth>rdepth?ldepth+1:rdepth+1; }
方法二:(递归法)
class Solution { public: int sumNumbers(TreeNode* root) { sum = 0; sumStep(root, 0); return sum; } private: int sum; void sumStep(TreeNode *root, int num){ if(!root) return; if(!root->left && !root->right) { sum = sum + num*10 + root->val; }else{ sumStep(root->left, num*10 + root->val); sumStep(root->right, num*10 + root->val); } } };
相关文章推荐
- Web开发在Mac下配置Apache、php、MySQL应该注意的地方(Yosemite10.10.3)
- java对象的克隆
- android developer官网不能打开怎么办
- 如果new 一个对象,用free去释放,会有什么问题?
- 写给未来宽带运营商的一封信(转)
- STM8中断的设置(ST Visual Develop和IAR环境下)
- datawindow 如何将grid格式改为free格式
- sqlite的limit使用
- myeclipse svn 修改用户名和密码
- Android触摸屏事件派发机制详解与源码分析二(ViewGroup篇)
- Android高德地图开发详解
- 《大话设计模式》--手机软件何时统一--桥接模式<Bridge>(22)
- 安卓开发一步到位
- 设计模式(建造者模式)
- arcmap生成泰森多边形
- Xcode进行ARC设置
- java 正则表达式入门(一)
- 冒泡排序(C++语言描述)
- nRF51822 LRC(32.768K) 内部RC和外部晶振的设置
- [OpenGL][SharpGL]用Polygon Offset解决z-fighting和stitching问题