国产探花免费观看_亚洲丰满少妇自慰呻吟_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);    }};


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汝城县| 兰西县| 西华县| 错那县| 德格县| 石狮市| 长治市| 阿巴嘎旗| 额敏县| 通渭县| 大化| 上蔡县| 台州市| 塔城市| 东丽区| 北川| 安平县| 凉城县| 本溪| 长顺县| 华蓥市| 蚌埠市| 玉田县| 陇西县| 梓潼县| 汾西县| 辛集市| 洛阳市| 德庆县| 鄂托克旗| 年辖:市辖区| 礼泉县| 上犹县| 沅江市| 武强县| 乌苏市| 祁阳县| 辽宁省| 筠连县| 万全县| 呈贡县|