题目:

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"]

求一个二叉树的所有路径

代码:

/**
* 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:
void RootPath(vector<string>& resultVec, TreeNode *root, string s)
{
if (root->left == NULL && root->right == NULL)
{
resultVec.push_back(s);
return;
}
if (root->left)
{
RootPath(resultVec, root->left, s + "->" + to_string(root->left->val));
}
if (root->right)
{
RootPath(resultVec, root->right, s + "->" + to_string(root->right->val));
}
} vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
if (root == NULL) return result;
RootPath(result, root, to_string(root->val));
return result;
}
};

[LeetCode257]Binary Tree Paths的更多相关文章

  1. [LintCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2 ...

  2. LintCode Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...

  3. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  4. &lt;LeetCode OJ&gt; 257. Binary Tree Paths

    257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...

  5. LeetCode_257. Binary Tree Paths

    257. Binary Tree Paths Easy Given a binary tree, return all root-to-leaf paths. Note: A leaf is a no ...

  6. [Swift]LeetCode257. 二叉树的所有路径 | Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  7. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  8. leetcode : Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  9. Leetcode 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

随机推荐

  1. 使用 sphinx 制作简洁而又美观的文档

    使用 sphinx 制作简洁而又美观的文档 使用 sphinx 制作简洁而又美观的文档

  2. HDU 4022 Bombing (map + multiset)

    题意: 在x,y坐标范围为10 ^ -9 ~~ 10 ^ 9的坐标轴之中,有 10W个点(注意有些点可能在同一坐标上),然后有10W个询问,处理询问按照输入顺序处理,对于每个询问a,b    a == ...

  3. poj3295 Tautology , 计算表达式的值

    给你一个表达式,其包括一些0,1变量和一些逻辑运算法,让你推断其是否为永真式. 计算表达式的经常使用两种方法:1.递归: 2.利用栈. code(递归实现) #include <cstdio&g ...

  4. hdu1542(线段树——矩形面积并)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 分析:离散化+扫描线+线段树 #pragma comment(linker,"/STA ...

  5. pygame系列_font游戏字体

    在pygame游戏开发中,一个友好的UI中,漂亮的字体是少不了的 今天就给大伙带来有关pygame中字体的一些介绍说明 首先我们得判断一下我们的pygame中有没有font这个模块 1 if not ...

  6. 【转】.net IL 指令解释速查

    名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上. Add.Ovf.Un 将两个无符号整数值相加,执行溢出检查,并且 ...

  7. hdu3570, 超级简单的斜率优化dp

    dp[i] = dp[j] + (a[i] - a[j])^2 + m;展开得 dp[i] = min{dp[j] + a[i]^2 + a[j]^2 - 2*a[i]*a[j] + m}其中a[i] ...

  8. php函数serialize()与unserialize() 数据序列化与反序列化

    php函数serialize()与unserialize()说明及案例.想要将已序列化的字符串变回 PHP 的值,可使用unserialize().serialize()可处理除了resource之外 ...

  9. windows phone 了解LinearGradientBrush类和RadialGradienBrush类(11)

    原文:windows phone 了解LinearGradientBrush类和RadialGradienBrush类(11) 我们了解到在能在xaml中完成的设计,一般在隐藏文件中也可通过代码完成: ...

  10. USACO sprime

    /* ID:kevin_s1 PROG:sprime LANG:C++ */ //N = 1仅仅可能为2,3,5,7.N > 1仅仅可能为1,3,7.9. N = i,到N = i + 1递推假 ...