leetcode Binary Tree Paths 257
来源:互联网 发布:可延迟服务器调度算法 编辑:程序博客网 时间:2024/06/05 14:54
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 {private: vector<string> vc;public: vector<string> binaryTreePaths(TreeNode* root) { vc.clear(); if(root==NULL) return vc; string path=to_string(root->val); dfs(root,path); return vc; } void dfs(TreeNode* root,string s){ if(root->left==NULL && root->right==NULL) { vc.push_back(s); return; } if(root->left!=NULL) dfs(root->left,s+"->"+to_string(root->left->val)); if(root->right!=NULL) dfs(root->right,s+"->"+to_string(root->right->val)); }};
0 0
- 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
- leetcode-257-Binary Tree Paths
- LeetCode 257Binary Tree Paths
- LeetCode 257 Binary Tree Paths
- leetcode 257 Binary Tree Paths
- LeetCode-257:Binary Tree Paths
- 测试网站兼容性:http://browsershots.org/ 和https://www.browserstack.com/
- 设计模式(十二)——代理模式
- Android常用开源库(框架)整合
- Spring Boot-Spring Tool Suite工具的安装
- 【Java修饰符】(3)final修饰符详解
- leetcode Binary Tree Paths 257
- Mysql的主主备份与主从备份--同步
- swift 保存照片到手机相册中
- 正则表达式必知必会(一)
- Android中使用开源框架android-image-indicator实现图片轮播部署
- 对ArrayList进行分页
- 2004 饶毅,鲁白, 邹承鲁:中国科技需要的根本转变
- 数据结构一绪论(上)之 b计算模型
- Spring(十一)AspectJ框架开发AOP(基于xml)