[LeetCode]113. Path Sum II(列出二叉树根到叶路径和等于sum的所有路径)

113. Path Sum II

原题链接
相似题目题解:112. Path Sum && 437. Path Sum III

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]
]

题目大意:
给一个二叉树和一个数sum, 列出二叉树中从根节点到叶子节点的路径和等于sum的所有路径

思路:

  • 运用深度优先遍历,结合112. Path Sum的题目,用二维数组paths存储所有符合的路径,用temp存储每次遍历的路径,符合条件就存进paths中

    代码1:(16ms)

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
//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:
    vector<vector<int>> pathSum(TreeNode* root, int sum) {//16ms
        vector<vector<int>> paths; //存储所有满足条件的路径
        vector<int> temp;//存储当前遍历路径
        help(root, paths, temp, sum);
        return paths;
    }
    void help(TreeNode* &root, vector<vector<int>> &paths, vector<int> temp,  int sum) {
        if(root == nullptr)
            return ;
        temp.push_back(root->val);
        if(root->left==nullptr && root->right==nullptr && sum==root->val)
            paths.push_back(temp);
        if(root->left != nullptr)
            help(root->left, paths, temp, sum-root->val);
        if(root->right != nullptr)
            help(root->right, paths, temp, sum-root->val);
    }
};
// 创建二叉树
TreeNode* CreateTreeByLevel(vector<int> num){
    int len = num.size();
    if(len == 0){
        return NULL;
    }//if
    queue<TreeNode*> queue;
    int index = 0;
    // 创建根节点
    TreeNode *root = new TreeNode(num[index++]);
    // 入队列
    queue.push(root);
    TreeNode *p = NULL;
    while(!queue.empty() && index < len){
        // 出队列
        p = queue.front();
        queue.pop();
        // 左节点
        if(index < len && num[index] != -1){
            // 如果不空创建一个节点
            TreeNode *leftNode = new TreeNode(num[index]);
            p->left = leftNode;
            queue.push(leftNode);
        }
        index++;
        // 右节点
        if(index < len && num[index] != -1){
            // 如果不空创建一个节点
            TreeNode *rightNode = new TreeNode(num[index]);
            p->right = rightNode;
            queue.push(rightNode);
        }
        index++;
    }//while
    return root;
}

int main()
{
    Solution s;
    // -1代表NULL
    vector<int> num = {5,4,8,11,-1,13,4,7,2,-1,-1,5,1};
    TreeNode* root = CreateTreeByLevel(num);
    vector<vector<int> > paths = s.pathSum(root,22);
    for(int i = 0;i < paths.size();i++){
        for(int j = 0;j < paths[i].size();j++){
            cout<<paths[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

代码2:(9ms)

  • 发现更好的解法,每次遍历完左树及时删除元素效率更高
  • 需要注意end函数用法
  • end函数:
    函数原型:
    iterator end();
    const_iterator end();
    功能:
    返回一个当前vector容器中末尾元素的迭代器。
    vector<vector<int>> pathSum1(TreeNode* root, int sum) {//9ms
        vector<vector<int>> paths; //存储所有满足条件的路径
        vector<int> temp;//存储当前遍历路径
        help(root, paths, temp, sum);
        return paths;
    }
    void help1(TreeNode* &root, vector<vector<int>> &paths, vector<int> &temp,  int sum) {
        if(root == nullptr)
            return ;
        temp.push_back(root->val);
        if(root->left==nullptr && root->right==nullptr && sum==root->val)
            paths.push_back(temp);
        if(root->left != nullptr)
            help1(root->left, paths, temp, sum-root->val);
        if(root->right != nullptr)
            help1(root->right, paths, temp, sum-root->val);
        //注意temp.end()指向的是最后一个元素的下一个位置,所以访问最后一个元素的正确操作为:temp.end() -1;
        //temp.erase()  删除某个元素
        temp.erase(temp.end() - 1);
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值