Leetcode63: Binary Tree Paths
来源:互联网 发布:协同过滤推荐算法综述 编辑:程序博客网 时间:2024/06/08 07:33
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"]
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
/** * 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: void dfs(TreeNode* Node, string item, vector<string>& result) { if(Node == NULL) return; item = item + std::to_string(Node->val); if(Node->left) dfs(Node->left, item+"->", result); if(Node->right) dfs(Node->right, item+"->", result); if(!Node->left && !Node->right) result.push_back(item); } vector<string> binaryTreePaths(TreeNode* root) { vector<string> str; if(root == NULL) return str; dfs(root, "", str); return str; }};
0 0
- Leetcode63: Binary Tree Paths
- LeetCode63:Unique Paths II
- Leetcode63 Unique Paths II
- LeetCode63. Unique Paths II
- leetcode63-Unique Paths II
- Leetcode63. Unique Paths II
- Leetcode63: Unique Paths II
- leetcode63. Unique Paths II
- leetcode63. Unique Paths II
- LeetCode63 Unique Paths II
- Binary Tree Paths
- Leetcode: Binary Tree Paths
- [LeetCode] Binary Tree Paths
- [Leetcode]Binary Tree Paths
- leetcode:Binary Tree Paths
- Binary Tree Paths
- [leetcode] Binary Tree Paths
- 257 Binary Tree Paths
- 前端收集(前端学习资源)
- transform-origin CSS3 旋转
- 版本控制系统小结
- APACHE日志参数说明
- 内核导言之文件子系统概貌
- Leetcode63: Binary Tree Paths
- mysql链接大量出错
- JQ自带的AjaxForm提交
- C#连接4种类型数据库(Access、SQL Server、Oracle、MySQL)
- iOS 9 iOS9 HTTP 不能正常使用的解决办法
- Leetcode64: Lowest Common Ancestor of a Binary Search Tree
- IEnumerable及IEnumerator接口
- ORACLE常用SQL语句大全
- Javascript学习笔记【第三章】3