LeetCode 257 Bianry Tree Paths(二叉树路径保存)
来源:互联网 发布:macbook不能下载软件 编辑:程序博客网 时间:2024/06/05 05:12
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> ans; if(root != nullptr) dfs(root, "", ans); return ans; }private: void dfs(TreeNode* root, string path, vector<string>& ans) { if(root->left == nullptr && root->right == nullptr) { ans.push_back(path + to_string(root->val)); return ; } if(root->left) dfs(root->left, path + to_string(root->val) + "->", ans); if(root->right) dfs(root->right, path + to_string(root->val) + "->", ans); } };
阅读全文
0 0
- LeetCode 257 Bianry Tree Paths(二叉树路径保存)
- 【Leetcode】257 Binary Tree Paths 二叉树的路径
- LeetCode OJ 之 Binary Tree Paths(二叉树路径)
- Binary Tree Paths(二叉树的路径)
- 257. Binary Tree Paths(打印二叉树所有路径)
- Binary Tree Paths (二叉树路径遍历)
- leetcode解题之57. Binary Tree Paths&129. Sum Root to Leaf Numbers Java版 (二叉树路径)
- 257.leetcode Binary Tree Paths(easy)[二叉树深度路径遍历]
- Tree----- 257. Binary Tree Paths(二叉树从根节点开始的所有路径)
- lintcode binary-tree-paths 二叉树的所有路径
- 257. Binary Tree Paths 二叉树的路径
- 257. Binary Tree Paths | 二叉树的路径
- 257. Binary Tree Paths打印二叉树路径
- Binary Tree Paths 二叉树的路径(含创建树的代码)
- 480.Binary Tree Paths-二叉树的所有路径(容易题)
- 257. Binary Tree Paths (求二叉树中所有根到叶子的路径)
- [LeetCode] Binary Tree Paths - 二叉树基础系列题目
- leetcode 257. Binary Tree Paths-树路径|深度搜索
- xls xlsx csv 导出datatable
- SVN 简单解决 database disk image is malformed 故障
- Emmet前端开发神器
- java中静态代码块的用法 static用法详解
- SpringMVC @RequestBody接收Json对象字符串 例子
- LeetCode 257 Bianry Tree Paths(二叉树路径保存)
- Zabbix系列之Zabbix安装搭建及汉化
- PyCharm使用技巧:Regex Tester(正则表达式测试工具)
- intellij idea 取消右侧中线
- pypy确实要比我们平时用的python快
- 百炼:4080:Huffman编码树
- POJ 1002 (字符串 && 模拟)
- Python 学习日知录(四)- 配置文件初步
- Redis集群使用总结(二)