[LeetCode257]Binary Tree Paths

时间:2022-09-18 18:30:05

题目:

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. &lbrack;LintCode&rsqb; 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&period; Binary Tree Paths

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

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

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

  5. LeetCode&lowbar;257&period; 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. &lbrack;Swift&rsqb;LeetCode257&period; 二叉树的所有路径 &vert; Binary Tree Paths

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

  7. &lbrack;LeetCode&rsqb; Binary Tree Paths 二叉树路径

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

  8. leetcode &colon; Binary Tree Paths

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

  9. Leetcode 257&period; Binary Tree Paths

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

随机推荐

  1. poj3484 Showstopper 二分

    题目地址 二分用的很是巧妙!关键是抽象出问题本质. #include <cstdio> #include <string> #include <cstring> ; ...

  2. 89&period; Gray Code

    题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...

  3. cordova&sol;phonegap&sol;webapp性能优化方法

    1.有条件可以自己做UI,不要用框架.用框架的话不要用jquery mobile,用sencha touch或者jqmobi(app framework) 2.不要在服务器生成UI,在本地生成. 3. ...

  4. easyui获取正在编辑行的代码

    easyui获取正在编辑行的代码……没这个真不知道怎么搞0.0可能这问题还要弄半天……卧槽 ...等于是笔记下来 :  var ed = $("dg").datagrid('get ...

  5. linux任务计划及周期性任务计划

    相关命令:at.batch.cron.mailx 未来某时间执行一次任务:at, batch 周期性运行某任务: cron 一.未来某时间执行一次任务:at命令 at, batch, atq, atr ...

  6. 通过 python-xmp-toolkit 读取图片xmlp信息

    这个模块使用很简单,下面是示例: file_name = '/path/to/xxx.JPG' from libxmp import XMPFiles, constsxmpfile = XMPFile ...

  7. Fortinet Security Fabric

    Fortinet Security Fabric 这个世界从不固步自封.在技术方面,这意味着解决方案供应商必须保持不断创新和探索才能实现生存与发展. 在网络安全领域,这更是至理名言.许多黑客都是才华横 ...

  8. 推荐两款富文本编辑器:NicEdit和Kindeditor

    做过Web开发的朋友相信都使用过富文本编辑器,比较出名的CuteEditor和CKEditor很多人应该已经使用过,在功能强大的同时需要加载的东西也变得很多.下面要推荐的两款富文本编辑器都是使用JS编 ...

  9. uversion5 怎么添加设备

    实时 点击网址去它的官网下载,然后选择自己的设备组,Dfg ,下载下来的是一个安装包,直接安装即可

  10. DatePickerDialog日期对话框以及回调函数的用法

    DatePickerDialog类的实例化需要用到回调接口,如下定义: android.app.DatePickerDialog.DatePickerDialog(Context context, O ...