Sum Root to Leaf Numbers 根节点到叶子组成数的和
来源:互联网 发布:淘宝网法院拍卖确认书 编辑:程序博客网 时间:2024/05/29 02:34
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
.
/** * 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 solve(root,0); } int solve(TreeNode *root,int sum) { if(root==NULL) { return 0; } if(root->left==NULL&&root->right==NULL) { return sum*10+root->val; } return solve(root->left,sum*10+root->val)+solve(root->right,sum*10+root->val); }};
0 0
- Sum Root to Leaf Numbers 根节点到叶子组成数的和
- Sum Root to Leaf Numbers 从根到叶子结点的和
- 从根到叶子的和 sum root to leaf numbers
- Sum Root to Leaf Numbers 求所有二叉树根到叶子节点路径之和 @LeetCode
- 【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(所有根到叶子结点组组成的数字相加)】
- LeetCode | Sum Root to Leaf Numbers(所有根结点到叶节点路径和的和)
- leetcode 129. Sum Root to Leaf Numbers-根到叶子之和|bfs|dfs|递归|非递归
- Sum Root to Leaf Numbers 从根到叶的和
- Sum Root to Leaf Numbers 二叉树的path 和
- LeetCode 129. Sum Root to Leaf Numbers(节点求和)
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- Sum Root to Leaf Numbers
- 点卡充值(2)
- Word Ladder 字符的相似变化
- Android 键盘弹出和关闭事件
- seletor android
- /usr/bin/perl^M: 坏的解释器: 没有那个文件或目录
- Sum Root to Leaf Numbers 根节点到叶子组成数的和
- php通过统一发放tickets实现单点登陆SSO功能
- eclipse Indigo Helios Galileo几种版本的意思
- HDOJ 5222 Exploration
- OpenCascade开源库的使用
- Android 在应用中启动其他应用
- 【转】Druid、BoneCP、DBCP、C3P0等主流数据库对比
- 【LeetCode】168.Excel Sheet Column Title
- 迷宫问题深度优先搜索----栈与递归