LeetCode 257:Binary Tree Paths
2015-12-31 15:39
351 查看
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
All root-to-leaf paths are:
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
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.
//简单的二叉树遍历,遍历的过程中记录之前的路径,一旦遍历到叶子节点便将该路径加入结果中。 class Solution { public: vector<string> binaryTreePaths(TreeNode* root) { vector<string> res; if (root==NULL) return res; binaryTreePaths(res, root, to_string(root->val)); return res; } void binaryTreePaths(vector<string>& result, TreeNode* node, string s) { if (node->left==NULL && node->right==NULL) { result.push_back(s); return; } if (node->left) binaryTreePaths(result, node->left, s + "->" + to_string(node->left->val)); if (node->right) binaryTreePaths(result, node->right, s + "->" + to_string(node->right->val)); } };
相关文章推荐
- 常规功能和模块自定义系统 (cfcmms)—029开发日志(创建ManyToMany的column4)
- java网络协议(IPV4/IPV6)
- Tomcat--系统架构与设计模式,第 1 部分: 工作原理
- 判断网络是否可用
- 报错: org.apache.commons.pool2.impl.GenericObjectPoolConfig not found
- INSTALL_FAILED_MEDIA_UNAVAILABLE错误处理
- [背景分离] 识别移动物体基于高斯混合 MOG
- dropdownlist显示树形结构
- 1217 实验四 递归下降语法分析程序设计
- 1231实验四 递归下降语法分析程序设计
- segment fault本质
- 使用路由器和花生壳如何在个人电脑上架构网站,通过域名访问 - 待整理
- ubuntu安装php
- 25 个 Java 机器学习工具和库
- JqMobi 基础知识
- C#反射是什么
- Spring mvc——接收对象Json数据,<mvc:annotation-driven/><context:annotation-config/><context:component-scan/>
- UIScrollView配合定时器和手动实现图片轮播无限循环
- POJ 3565 Ants
- [Java][MyBatis]物理分页实现