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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

LintCode 480:Binary Tree Paths

2019-11-10 20:13:57
字體:
供稿:網(wǎng)友

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

Solution:每個(gè)結(jié)點(diǎn)記錄從root至其的path,當(dāng)結(jié)點(diǎn)無左右孩子時(shí),將path傳入進(jìn)vector。

這里需要注意的是,recordPath函數(shù)中,path記錄著root至結(jié)點(diǎn)的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;    }};


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 金华市| 古田县| 萨嘎县| 阳新县| 彭阳县| 张家界市| 洛阳市| 洪湖市| 石景山区| 浮梁县| 稻城县| 龙游县| 赫章县| 兴隆县| 平谷区| 杭锦旗| 冷水江市| 凯里市| 呼图壁县| 莲花县| 望城县| 兴文县| 宜宾市| 高陵县| 甘洛县| 封开县| 视频| 布尔津县| 罗定市| 郯城县| 九台市| 江城| 雷波县| 安吉县| 疏勒县| 安吉县| 马鞍山市| 宜宾县| 垦利县| 龙泉市| 铜山县|