Path Sum II

原创 2015年11月20日 15:52:06
  1. 问题

    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]
    ] 
    
  2. 解答
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution1 {
    public:
        vector<vector<int>> pathSum(TreeNode* root, int sum) {
     
             vector<int> a;
             path(root,sum,a);
           
            return res;
        }
        void path(TreeNode *root,int sum,vector<int> a)
        {
             if(root)
            {
           // if(root==NULL) return res;
              if(root->left==NULL && root->right==NULL && sum==root->val)
              {
                  a.push_back(root->val);
                  res.push_back(a);
                  //a.clear();
              }
             else
              {
                a.push_back(root->val);
                path(root->left,sum-root->val,a);
                path(root->right,sum-root->val,a);
              }
            }
        }
    public:
       vector<vector<int>> res;
    };
    
    class Solution2 {
    public:
        vector<vector<int>> pathSum(TreeNode* root, int sum) {
     
            // vector<int> a;
             path(root,sum);
           
            return res;
        }
        void path(TreeNode *root,int sum)
        {
             if(root)
            {
           // if(root==NULL) return res;
              if(root->left==NULL && root->right==NULL && sum==root->val)
              {
                  a.push_back(root->val);
                  res.push_back(a);
                  return ;
                  //a.clear();
              }
             else
              {
                   a.push_back(root->val);
                   
                  if(root->left)
                  {
                       
                    path(root->left,sum-root->val);
                    a.pop_back();
                       
                  }
                   
                 if(root->right)
                 {
                    path(root->right,sum-root->val);
                    a.pop_back();
                 }
              }
            }
        }
    public:
       vector<vector<int>> res;
       vector<int> a;
    };
    
    class Solution {
    public:
        Solution():a(1000){}
        vector<vector<int>> pathSum(TreeNode* root, int sum) {
     
             path(root,sum,0);
           
            return res;
        }
        void path(TreeNode *root,int sum,int n)
        {
            if(root==NULL) return ;
             if(root)
            {
               a[n]=root->val;
               n++;
               
              if(root->left==NULL && root->right==NULL && sum==root->val)
              {
                  vector<int> temp(a.begin(),a.begin()+n);
                  res.push_back(temp);
                  return ;
              }
             else
              {
         
                    path(root->left,sum-root->val,n);
         
                    path(root->right,sum-root->val,n);
     
              }
            }
        }
    public:
       vector<vector<int>> res;
       vector<int> a;
    };


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【leetcode】path-sum-ii

huale //2014年8月23日11:16:49 //2014年8月23日11:45:25 //修改上个代码即可,在处理返回值时浪费很长时间 #include #include u...

[leetcode]24 Path Sum II

题目链接:https://leetcode.com/problems/path-sum-ii/ Runtimes:93ms 1、问题 Given a binary tree and a sum,...
  • shawjan
  • shawjan
  • 2015年03月11日 13:45
  • 260

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 su...

【LeetCode】path-sum i&ii

题干path-sum iGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that a...

LeetCode 1.Minimum Path Sum 2.Unique Paths I and II

大家好,我是刘天昊,快到端午节了,今天说两道动态规划的题目(话说动规真的挺难的) 当然这三题是一样的解体思路先看Unique Paths  A robot is located a...

[Leetcode 113, Medium] Path sum II

[Leetcode 113, Medium] Path sum II

56. Merge Intervals\113. Path Sum II\211. Add and Search Word - Data structure design

Merge Intervals 题目描述 代码实现 Path Sum II 题目描述 代码实现56. Merge Intervals题目描述Given a collection of interval...

LeetCode | Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su...

Leetcode[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 su...

树-Path Sum II(指定和,求根到叶子的路径)

题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Path Sum II
举报原因:
原因补充:

(最多只允许输入30个字)