113. Path Sum II
来源:互联网 发布:网络在线平交流平台 编辑:程序博客网 时间:2024/05/18 06:08
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,
/** * 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: bool hasPathSum(struct TreeNode* root, int sum,vector<int> &myVec,vector<vector<int>> &listVec) { if(root == NULL){ return false; } //this node is leaf node if((!root->left)&&(!root->right)){ if(sum == root->val){ //push this vector; myVec.push_back(root->val); listVec.push_back(myVec); myVec.pop_back(); return true; } }//leaf Node if(root->left){ myVec.push_back(root->val); hasPathSum(root->left,sum-root->val,myVec,listVec); myVec.pop_back(); } if(root->right){ myVec.push_back(root->val); hasPathSum(root->right,sum-root->val,myVec,listVec); myVec.pop_back(); } return true; } vector<vector<int>> pathSum(TreeNode* root, int sum) { vector<vector<int>> result; vector<int> myVec; hasPathSum(root,sum,myVec,result); return result; }};
阅读全文
0 0
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 通过金矿模型介绍动态规划
- 微信小程序开发过程中遇到的问题
- CAN总线与CANOPEN协议
- POJ 1321
- 图文详解 IntelliJ IDEA 15 创建普通 Java Web 项目
- 113. Path Sum II
- 多文件上传
- JS中的表格排序和关于DOM映射机制
- echarts中画布的清空
- [编程题] 最大的奇约数
- [Leetcode] 576. Out of Boundary Paths
- bfs 大逃亡
- 「LibreOJ β Round #4」求和 莫比乌斯函数
- 调用摄像头功能的方法详解