LeetCode 257. Binary Tree Paths
来源:互联网 发布:vb语言基本代码 编辑:程序博客网 时间:2024/06/07 03:14
257. Binary Tree Paths
Description
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
All root-to-leaf paths are:
[“1->2->5”, “1->3”]
Analysis
这道题的求出所有从根到叶子节点的理。
我的做法是先记录路径,然后当每一次路径都走到叶子节点的时候,就将该路径记录到向量中,最后得到答案。
Code
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ //find one path and then push backclass Solution {public: void fun(TreeNode* root,string path,vector<string>&res){ if(root->left==NULL&&root->right==NULL){ res.push_back(path); return ; } if(root->left) fun(root->left,path+"->"+to_string(root->left->val),res); if(root->right) fun(root->right,path+"->"+to_string(root->right->val),res); } vector<string> binaryTreePaths(TreeNode* root) { vector<string> res; if(root == NULL) return res; string path = to_string(root->val); fun(root,path,res); return res; }};
0 0
- [leetcode] 257. Binary Tree Paths
- 257. Binary Tree Paths LeetCode
- 【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
- LeetCode 257. Binary Tree Paths
- 257.[Leetcode]Binary Tree Paths
- LeetCode 257. Binary Tree Paths
- leetcode 257. Binary Tree Paths
- Java安全之消息摘要
- 学习OpenCV——KeyPoint Matching 优化方式
- 棋盘覆盖问题
- Servlet API中包装类装饰模式的应用
- rediskey的辅助类备忘
- LeetCode 257. Binary Tree Paths
- solvepnp详解
- Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
- java中“或”(||)条件的执行次序
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
- spring boot 单元测试
- quick框架之MyApp详解
- 正则表达式-元字符
- RxJava—组合操作符