国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LintCode 480:Binary Tree Paths

2019-11-10 19:09:54
字體:
來源:轉載
供稿:網友

PRoblem:Given a binary tree, return all root-to-leaf paths.

Solution:每個結點記錄從root至其的path,當結點無左右孩子時,將path傳入進vector。

這里需要注意的是,recordPath函數中,path記錄著root至結點的path,不可改變,不能加&(reference),而pathResult需要添加新的path,所以要加&。

void recordPath(TreeNode* tn,vector<int> path,vector< vector<int> >& pathResult)

class Solution {    void recordPath(TreeNode* tn,vector<int> path,vector< vector<int> >& pathResult){        path.push_back(tn->val);        if(!tn->left&&!tn->right)            pathResult.push_back(path);        if(tn->left)            recordPath(tn->left,path,pathResult);        if(tn->right)            recordPath(tn->right,path,pathResult);        return;    }    void convert(ve,vector< vector<int> > path)ctor<string>& result{

        int rowSize=path.size();        for(int i=0;i<rowSize;i++){            string tmp;            tmp=to_string(path[i][0]);            for(int j=1;j<path[i].size();j++){                tmp+="->";                tmp+=to_string(path[i][j]);            }            result.push_back(tmp);        }        return;    }public:    /**     * @param root the root of the binary tree     * @return all root-to-leaf paths     */    vector<string> binaryTreePaths(TreeNode* root) {        vector<string> result;        vector< vector<int> > pathResult;        vector<int> tmp;        if(!root)            return result;        recordPath(root,tmp,pathResult);        convert(result,pathResult);        return result;    }};


上一篇:tomcat之連接器

下一篇:poj1543

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泸定县| 邹城市| 威海市| 揭西县| 壤塘县| 天门市| 常宁市| 西城区| 汶川县| 常州市| 金秀| 巴南区| 邹城市| 政和县| 桓台县| 昆山市| 永寿县| 旬邑县| 康平县| 黔东| 镇原县| 鹤壁市| 镇宁| 本溪市| 明水县| 英山县| 彰化市| 淳安县| 历史| 林口县| 铜鼓县| 阜平县| 密山市| 时尚| 马尔康县| 昆明市| 赤城县| 普陀区| 泰顺县| 南昌市| 榆林市|