Binary Tree Paths -- leetcode
来源:互联网 发布:上瘾网络剧第二季泰国 编辑:程序博客网 时间:2024/04/29 19:16
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"]
基本思路
中序遍历。
维持一条路径,碰到叶子节点进行输出。
/** * 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; dfs(ans, root, ""); return ans; } void dfs(vector<string>& ans, TreeNode* root, string path) { if (!root) return; path += to_string(root->val); if (!root->left && !root->right) return ans.push_back(path); path += "->"; dfs(ans, root->left, path); dfs(ans, root->right, path); }};
0 0
- Leetcode: Binary Tree Paths
- [LeetCode] Binary Tree Paths
- [Leetcode]Binary Tree Paths
- leetcode:Binary Tree Paths
- [leetcode] Binary Tree Paths
- Leetcode: Binary Tree Paths
- LeetCode:Binary Tree Paths
- leetcode Binary Tree Paths
- leetcode Binary Tree Paths
- LeetCode:Binary Tree Paths
- LeetCode Binary Tree Paths
- LeetCode Binary Tree Paths
- LeetCode Binary Tree Paths
- LeetCode || Binary Tree Paths
- *LeetCode-Binary Tree Paths
- Binary Tree Paths -- leetcode
- LeetCode---Binary Tree Paths
- leetcode-Binary Tree Paths
- Git 使用GitHub
- LintCode | Easy | 翻转二叉树 | Invert Binary Tree
- Debug---Eclipse断点调试基础
- Git 自定义Git
- Ubuntu 15.04趟坑笔记
- Binary Tree Paths -- leetcode
- uva12167等价性证明
- java制作视频播放器
- Git 忽略特殊文件
- 用顺序表调整la的左右两边,左边元素全部为奇数,右边元素全部为偶数
- SQL 根据连续区间 生成数据。 区间拆分
- 乱序字符串
- LintCode | Easy | 删除排序链表中的重复元素 | Remove Duplicates from Sorted List
- Git 配置别名