113. Path Sum II

可莉
• 阅读 453

欢迎fork and star:Nowcoder-Repository-github

113. Path Sum II

题目

 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:
Given the below binary tree and sum = 22,

              5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1

return

[
   [5,4,11,2],
   [5,8,4,5]
]

解析

  • 采用dfs和bfs的方法,很经典的方法,类似的题目很多都可以采用此方法,熟练掌握!

    class Solution_113 { public:

    void dfs(TreeNode* root,int cur_sum,int sum,vector<int> &vec ,vector<vector<int>> &vecs)
    {
        if (!root)
        {
            return;
        }
    
        if (root->left==NULL&&root->right==NULL&&cur_sum==sum)
        {
            vecs.push_back(vec);
            return;
        }
        if (root->left)
        {
            vec.push_back(root->left->val);
            dfs(root->left, cur_sum + root->left->val, sum, vec, vecs);
            vec.pop_back();
        }
        if (root->right)
        {
            vec.push_back(root->right->val);
            dfs(root->right, cur_sum + root->right->val, sum, vec, vecs);
            vec.pop_back();
        }
        
        return;
    }
    
    vector<vector<int> > pathSum1(TreeNode *root, int sum) {
        vector<vector<int>> vecs;
        vector<int> vec;
    
        if (!root)
        {
            return vecs;
        }
    
        vec.push_back(root->val);
        dfs(root,root->val,sum,vec,vecs); //输入当前节点及其当前节点的和
        return vecs;
    }
    
    vector<vector<int> > pathSum(TreeNode *root, int sum) {
    
        vector<vector<int>> vecs;
    
        if (!root)
        {
            return vecs;
        }
    
        queue<TreeNode*> que;
        que.push(root);
    
        queue<vector<int>> path;
        path.push({ root->val }); 
    
        while (!que.empty())
        {
            TreeNode* temp;
            int size = que.size();
    
            for (int i = 0; i < size;i++)
            {
                temp = que.front();
                que.pop();
    
                vector<int>  vec= path.front();
                path.pop();
    
                if (temp->left==NULL&&temp->right==NULL&& accumulate(vec.begin(),vec.end(),0)==sum) //0 累加的初始值
                {
                    vecs.push_back(vec);
                }
                if (temp->left)
                {
                    que.push(temp->left);
    
                    vector<int> var = vec;
                    var.push_back(temp->left->val);
                    path.push(var);
                    //vec.pop_back();
                }
                if (temp->right)
                {
                    que.push(temp->right);
                    vector<int> var = vec;
                    var.push_back(temp->right->val);
                    path.push(var);
                    //vec.pop_back();
                }
            }
        }
        return vecs;
    }
    

    };

题目来源

点赞
收藏
评论区
推荐文章
Stella981 Stella981
2年前
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593
Wesley13 Wesley13
2年前
ES6的介绍和常用语法
本文最初发表于博客园,并在GitHub(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fsmyhvae%2FWeb)上持续更新前端的系列文章。欢迎在GitHub上关注我,一起入门和进阶前端。以下是正文。前言ECMAScript
Stella981 Stella981
2年前
Android自定义控件系列
Android自定义控件系列–Path综述项目源码点击查看详情(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fzhaolongs%2FCommonViewApplication)Path中文释义为路径然而它在
Stella981 Stella981
2年前
113. Path Sum II
欢迎forkandstar:NowcoderRepositorygithub(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Franjiewwen%2FNowcoder)113\.PathSumII
Stella981 Stella981
2年前
Golang注册Eureka的工具包goeureka发布
1.简介提供Go微服务客户端注册到Eureka中心。点击:github地址(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2FSimonWang00%2Fgoeureka),欢迎各位多多star!(已通过测试验证,用于正式生产部署)2.原理
Easter79 Easter79
2年前
ThreadLocal快速了解一下
欢迎点赞阅读,一同学习交流,有疑问请留言。GitHub上也有开源JavaHouse(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fbuerbl%2FJavaHouse)欢迎star1引入在Java8里面,ThreadLoc
Stella981 Stella981
2年前
Dear ImGUI 使用指南
文档1)DearIMGui(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Focornut%2Fimgui)2)知乎(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.
Stella981 Stella981
2年前
Essential Studio for UWP发布2017 v2,新增甘特图控件
EssentialStudioforUWP(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.evget.com%2Fproduct%2F3894)是包含有35组件的综合套包,包括最快的图表和网格组件。所有组件根据当前被呈现的设备系列自适应渲染。EssentialStu
Stella981 Stella981
2年前
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法参考文章:(1)Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.codeprj.com%2Fblo
Stella981 Stella981
2年前
OpenYurt 入门
!头图.png(https://ucc.alicdn.com/pic/developerecology/d35db6f1020a4b85bf70f437903c342d.png)作者| 唐炳昌来源|阿里巴巴云原生公众号(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fmp.