257 Binary Tree Paths
来源:互联网 发布:yy免费协议软件 编辑:程序博客网 时间:2024/06/05 07:57
题意:给定二叉树,找出所有根到叶子的路径
分析: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> ans; //if(root==nullptr) return ans; string curPath=""; dfs(ans,curPath,root); return ans; } string getstring ( const int n ){ std::stringstream newstr; newstr<<n; return newstr.str(); } void dfs(vector<string> &paths,string curPath,TreeNode* root){ if(root==nullptr) return; if(root->left==nullptr&&root->right==nullptr){ paths.push_back(curPath+getstring(root->val)); return; } dfs(paths,curPath+getstring(root->val)+"->",root->left); dfs(paths,curPath+getstring(root->val)+"->",root->right); }};
0 0
- 257 Binary Tree Paths
- 257 Binary Tree Paths
- 257 Binary Tree Paths
- [257]Binary Tree Paths
- 257、Binary Tree Paths
- 257 Binary Tree Paths
- leetcode 257: Binary Tree Paths
- leetCode #257 Binary Tree Paths
- LeetCode(257)Binary Tree Paths
- [leetcode 257]Binary Tree Paths
- leetcode[257] Binary Tree Paths
- LeetCode 257: Binary Tree Paths
- leetcode 257 : Binary Tree Paths
- 【LEETCODE】257-Binary Tree Paths
- [leetcode 257] Binary Tree Paths
- LeetCode 257 Binary Tree Paths
- LeetCode 257:Binary Tree Paths
- LeetCode 257:Binary Tree Paths
- 线性表
- Objective-C 基础知识之 (十六):内存管理原则一
- 大家好我又滚回来了
- hdu1213
- shell脚本学习 三
- 257 Binary Tree Paths
- 《机器学习与R语言》读书笔记1:机器学习简介
- STL学习之vector容器
- TestCase V2.0版本
- 讯飞语音万能五笔超强手写3合1输入法:
- Objective-C 基础知识之 (十七):内存管理原则二
- C#编写NotifyIcon 将窗口最小化到托盘
- Arraylist和Vector源码学习笔记
- NSURLSession和NSURLConnection比较