112. Path Sum
2017-02-27 21:47
232 查看
简单DFS
/** * 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<int> probVal(TreeNode* root) { vector<int> result; if(root==NULL) return result; if(root->left==NULL&&root->right==NULL) { result.push_back(root->val); return result; } vector<int> leftResult=probVal(root->left); vector<int> rightResult=probVal(root->right); for(int i=0;i<leftResult.size();i++) result.push_back(root->val+leftResult[i]); for(int i=0;i<rightResult.size();i++) result.push_back(root->val+rightResult[i]); return result; } bool hasPathSum(TreeNode* root, int sum) { vector<int> result=probVal(root); for(int i=0;i<result.size();i++) { if(result[i]==sum) return true; } return false; } };
相关文章推荐
- LeetCode 112 Path Sum
- Lettcode_112_Path Sum
- Java for LeetCode 112 Path Sum
- LeetCode 112 Path Sum
- 112. Path Sum
- Leetcode_112_Path Sum
- 112 Path Sum
- leetcode[112]:Path Sum
- LeetCode112—Path Sum
- leecode 解题总结:112. Path Sum
- Leetcode#112 Path Sum
- 【LeetCode从零单排】No112 Path Sum
- LeetCode 112 Path Sum
- LeetCode 112 Path Sum
- Leetcode 笔记 112 - Path Sum
- LeetCode 112: Path Sum
- #112 Path Sum
- LeetCode_112 Path Sum
- 112、path sum
- 【LEETCODE】112-Path Sum