leetcode---Sum Root to Leaf Numbers---树的深度遍历
来源:互联网 发布:网络聊天用语段子 编辑:程序博客网 时间:2024/04/28 09:59
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.
/** * 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 ans; void solve(TreeNode* root, int tmp) { if(root->left == NULL && root->right == NULL) ans += tmp; if(root->left) solve(root->left, tmp*10+root->left->val); if(root->right) solve(root->right, tmp*10+root->right->val); } int sumNumbers(TreeNode* root) { if(root == NULL) return 0; ans = 0; solve(root, root->val); return ans; }};
0 0
- leetcode---Sum Root to Leaf Numbers---树的深度遍历
- LeetCode 129: Sum Root to Leaf Numbers 树的遍历
- leetcode Sum Root to Leaf Numbers 树
- leetcode---sum-root-to-leaf-numbers---树
- 129.leetcode Sum Root to Leaf Numbers(medium)[深度遍历DFS]
- leetcode:Sum Root to Leaf Numbers (二叉树的DFS)
- 二叉树遍历129.Sum Root to Leaf Numbers
- leetcode 86: Sum Root to Leaf Numbers
- [LeetCode] Sum root to leaf numbers
- LeetCode : Sum Root to Leaf Numbers
- [leetcode] Sum Root to Leaf Numbers
- 【leetcode】Sum Root to Leaf Numbers
- LeetCode之sum root to leaf numbers
- leetcode:Sum Root to Leaf Numbers
- [LeetCode]Sum Root to Leaf Numbers
- [Leetcode]Sum Root to Leaf Numbers
- [leetcode]Sum Root to Leaf Numbers
- leetcode Sum Root to Leaf Numbers
- LVM逻辑卷管理
- Spring Tool Suite使用自带maven速度慢---修改settings.xml更新mirror方法
- JAVA字符数组 char[] 到字符串 String
- ceph的一些浅见——关于日志(1)
- 顺序建立链表
- leetcode---Sum Root to Leaf Numbers---树的深度遍历
- 求多个数的最小公倍数
- Java 内存管理机制与内存泄露
- Xcode8去除控制台多余打印(转)
- 2016ICPC北京站网络预选赛F hihoCoder1388 Periodic Signal,FFT|NTT+CRT
- Android之ViewDragHelper的使用
- Error:Authentication scheme ‘all’(Authentication) is not supported by protocol ‘file’
- bzoj 4326: NOIP2015 运输计划 树链剖分+二分+差分数组
- Java内存管理:深入Java内存区域