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.

有3个这个类型的题了,Path Sum ,Path Sum II还有这个

 

本来想直接传流的,但是流好像只能传递引用,所以最后还是传递了vector

C++代码实现:

#include<iostream>
#include<new>
#include<vector>
#include<sstream>
#include<cstdlib>
using namespace std;

//Definition for binary tree
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution
{
public:
    int sumNumbers(TreeNode *root) {
        vector<int> vec;
        vector<int> tmp;
        int sum=0;
        hasPathSum(root,vec,tmp);
        for(auto a:vec)
           sum+=a;
        return sum;
    }
//path传入引用是因为,每一个对path的操作都要有影响,而tmp作为参数传入,是因为将上层的修改带入下层,但是下层对tmp的修改不会影响上层的操作
    void hasPathSum(TreeNode *root,vector<int> &path,vector<int> tmp)
    {
        if(root==NULL)
            return;
        tmp.push_back(root->val);
        if(root->left==NULL&&root->right==NULL)
        {
            stringstream ss;
            for(size_t i=0;i<tmp.size();i++)
                ss<<tmp[i];
            string s=ss.str();
            int c=atoi(s.c_str());
            path.push_back(c);
        }
        if(root->left)
            hasPathSum(root->left,path,tmp);
        if(root->right)
            hasPathSum(root->right,path,tmp);
    }
    void createTree(TreeNode *&root)
    {
        int i;
        cin>>i;
        if(i!=0)
        {
            root=new TreeNode(i);
            if(root==NULL)
                return;
            createTree(root->left);
            createTree(root->right);
        }
    }
};
int main()
{
    Solution s;
    TreeNode *root;
    s.createTree(root);
    int sum=s.sumNumbers(root);
    cout<<sum<<endl;
}

运行结果:

 

时间: 2024-07-29 19:38:52

Sum Root to Leaf Numbers的相关文章

[LeetCode]129.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 /

【LeetCode从零单排】No129 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 / \

poj 2739 Sum of Consecutive Prime Numbers【素数筛】

我觉得这题的数据应该有些刁钻,一共至少提交了五次,一直是WA,无语了......用一个result数组存素数硬是不对.后来就算了,改用直接判断(法二),继续WA,后来发现是MAXN至少应为10002(被数据坑了),终于A掉了...... 后来继续改法一多次,任然WA,一直不清楚原因. 继续思考发现有一个很隐蔽的问题就是我后来用到   if(result[i]>n) break;    而result数组中 10000以内 最后一个素数是 9997,后面全是0了.这样无法停止,所以必须加一个大数作

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

LeetCode总结【转】

转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetcode.com的online judge中151道算法题目.除各个题目有特殊巧妙的解法以外,大部分题目都是经典的算法或者数据结构,因此做了如下小结,具体的解题思路可以搜索我的博客:LeetCode题解 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

[LeetCode]--112. Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 return tru

[LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

目录:1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次遍历从低往高输出 BFS 3.Maximum Depth of Binary Tree - 求二叉树的深度 DFS4.Balanced Binary Tree - 判断平衡二叉树 DFS5.Path Sum - 二叉树路径求和判断DFS 题目概述:Given a binary tree, return

ivy中文参考文档(13)-ant任务(1)-buildlist

buildlist任务用于获取按照ivy依赖信息从小到大排序的文件(通常是build.xml文件) 列表,或者相反(从1.2之后) 这个任务在结合subant构建相关项目集合时特别有效, 可以确保依赖在其他依赖它的模块之前被构建. 当你要排序的模块的ivy.xml不包含修订版本号,在依赖上定义的rev属性将不被使用. 当你要排序的模块的ivy.xml包含修订版本号,修订版本号将被使用.如果修订版本号和依赖描述不匹配,将会记录警告日志而模块被 认为是不同的模块. 从1.3版本起,root属性也可以