LeetCode] binaryTreePaths
class Solution {
public:
void binaryPath(TreeNode* root, vector<string>& result, string path) {
if (root == NULL) {
return;
}
if (path.empty()) {
path = to_string(root->val);
} else {
path += "->" + to_string(root-> val);
}
if (root->left == NULL && root->right == NULL) {
result.push_back(path);
return;
}
if (root->left) {
binaryPath(root->left, result, path);
}
if (root->right) {
binaryPath(root->right, result, path);
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
string path;
binaryPath(root, result, path);
return result;
}
};
LeetCode] binaryTreePaths的更多相关文章
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- LeetCode 笔记总结
前言 之前把一些LeetCode题目的思路写在了本子上,现在把这些全都放到博客上,以后翻阅比较方便. 题目 99.Recover Binary Search Tree 题意 Two elements ...
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode算法题-Binary Tree Paths(Java实现-3种解法)
这是悦乐书的第199次更新,第206篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第62题(顺位题号是257).给定二叉树,返回所有根到叶路径.例如: 输入: 1 / \ ...
- 一些leetcode算法题
DFS算法 思想:一直往深处走,直到找到解或者走不下去为止 DFS(dep,...) // dep代表目前DFS的深度 { if (找到解或者走不下去了){ return; } 枚举下种情况,DFS( ...
- leetcode算法总结
算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...
- Leetcode总结之DFS
package DFS; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; impo ...
- LeetCode刷题总结-树篇(上)
引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...
随机推荐
- ViewPager的简单用法+适配器+监听器的介绍
之前的actionbar+fragment文章中写过viewpager的简单用法,但因为是融合的文章,所以今天把viewpager提取出来写了.方便查询浏览~ 思路: 1.在布局文件中设置viewpa ...
- [Link]Gearman分布式任务处理系统
http://blog.csdn.net/jiao_fuyou/article/category/1745977 http://www.cnblogs.com/cocowool/archive/201 ...
- 使用pm2管理node.js应用
中文文档:https://pm2.io/doc/zh/runtime/quick-start/ pm2是从nodejs衍生出来的服务器进程管理工具,可以做到开机就启动nodejs.当然了,有些运维同学 ...
- matlib实现logistic回归算法(序一)
数据下载:http://archive.ics.uci.edu/ml/datasets/Adult 数据描述:http://archive.ics.uci.edu/ml/machine-learnin ...
- 一次问题追查----短字符串签名算法引发的bug
近期开发代码, 出现了一些诡异现象.追查原因是公司使用的签名函数出现的问题. 问题: 代码使用的签名库函数, 对于<=4字节的字符串, 签名就是本身. #include<stdio.h&g ...
- 如何移除EFI system partition?
莫名其妙, 在我的服务器上出现了这样一种分区, 上面写着EFI system, 删也删不掉, 因为删除分区的菜单是灰掉的. 找到了这篇文章, 成功的删掉了这个烦人的分区. 整个过程记录如下: 参考 ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Spiral Matrix leetcode java
题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spira ...
- java判断一个字符串是否包含某个字符
一.contains方法 1:描述 java.lang.String.contains() 方法返回true,当且仅当此字符串包含指定的char值序列 2:声明 public boolean cont ...
- jquery操作select(取值,设置选中)(转)
http://www.cnblogs.com/liaojie970/p/5210541.html 比如<select class="selector"></sel ...