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

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

LintCode 480:Binary Tree Paths

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

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

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

這里需要注意的是,recordPath函數(shù)中,path記錄著root至結(jié)點的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ā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 沙雅县| 泰和县| 衡阳市| 怀安县| 聂荣县| 旺苍县| 漳州市| 中超| 信丰县| 清镇市| 香港 | 七台河市| 寻甸| 伊春市| 安化县| 宣恩县| 天祝| 什邡市| 东乌珠穆沁旗| 海晏县| 龙陵县| 铜鼓县| 湟源县| 苏尼特左旗| 武清区| 梨树县| 宕昌县| 松原市| 翁牛特旗| 保靖县| 利辛县| 南城县| 华池县| 湛江市| 桂林市| 屯昌县| 汤原县| 乐至县| 苗栗县| 苏尼特左旗| 石门县|