Binary Tree Paths
2015-12-11 18:58
176 查看
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
All root-to-leaf paths are:
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> result; vector<string> binaryTreePaths(TreeNode* root) { if(root == NULL) return result; getPath(root,to_string(root->val)); return result; } void getPath(TreeNode* root,string path){ if(root->left == NULL && root->right == NULL){ result.push_back(path); } if(root->left != NULL) getPath(root->left,path+"->"+to_string(root->left->val)); if(root->right != NULL) getPath(root->right,path+"->"+to_string(root->right->val)); } };
相关文章推荐
- windows环境下Theano使用时出现ld.exe: cannot find -lblas
- myeclipse 2015 SECURITY ALERT INTEGERITY CHECK ERROR
- 20点的full mesh的ospf协商
- hdu5135 Little Zu Chongzhi's Triangles
- HBuilder开发App简介
- Android学习第五天————AdapterViewFlipper组件
- iOS绘制字符串和图片
- caffe源码之blob.cpp
- mybatis的sql注入问题
- 【玩树莓】SimpleCV中使用Pi Camera和手机摄像头
- 第一篇博客,Hello World
- 盘点过去一年,MIT人工智能实验室的那些创新
- Jenkins构建Android项目持续集成之创建项目
- 你应该掌握的七种回归技术
- MySQL在大数据Limit使用
- 判断手机电脑
- Redis 作为缓存服务器的配置
- SSH工具中文乱码
- mysql多表关联更新和in效率问题
- C++11 学习笔记 列表初始化