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

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

LeetCode 257. Binary Tree Paths

2019-11-08 02:56:35
字體:
來源:轉載
供稿:網友

題目鏈接:https://leetcode.com/PRoblems/binary-tree-paths/?tab=Description

題目描述:

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

For example, given the following binary tree:

   1 /   /2     3 /  5

All root-to-leaf paths are:

["1->2->5", "1->3"]思路:題目很簡單,直接用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<string> binaryTreePaths(TreeNode* root) {        vector<string> result;        string tmpPath="";        DFS(root,tmpPath,result);        return result;    }    void DFS(TreeNode* root,string path,vector<string> &Paths)    {        if(root==NULL)             return;        if(root->left==NULL&&root->right==NULL)        {            path+=to_string(root->val);            Paths.push_back(path);            return;        }        path+=to_string(root->val);        path+="->";        DFS(root->left,path,Paths);        DFS(root->right,path,Paths);    }};


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 合江县| 锡林郭勒盟| 牙克石市| 新龙县| 尼勒克县| 社旗县| 七台河市| 双柏县| 都安| 深水埗区| 潍坊市| 阿合奇县| 诸暨市| 合作市| 凤山市| 孟州市| 大埔县| 日土县| 汉川市| 巴林右旗| 黄陵县| 中牟县| 尉犁县| 郑州市| 武冈市| 镇安县| 宁南县| 普兰县| 惠来县| 女性| 南漳县| 汉阴县| 苍梧县| 江永县| 绵竹市| 靖宇县| 南城县| 临泉县| 中江县| 古丈县| 福贡县|