480-二叉树的所有路径

给一棵二叉树,找出从根节点到叶子节点的所有路径。

您在真实的面试中是否遇到过这个题? Yes

样例

给出下面这棵二叉树:



所有根到叶子的路径为:

[

"1->2->5",

"1->3"

]

标签

二叉树 谷歌 二叉树遍历 脸书

思路

使用深度优先搜索 + 回溯

code

/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root the root of the binary tree
* @return all root-to-leaf paths
*/
vector<string> binaryTreePaths(TreeNode* root) {
// Write your code here
if (root == NULL) {
return vector<string>();
}
vector<string> result;
vector<int> path;
DFS(root, path, result);
return result;
} void DFS(TreeNode * root, vector<int> &path, vector<string> &result) {
path.push_back(root->val);
if (root->left == NULL && root->right == NULL) {
string temp;
for (int p : path) {
char pa[256];
sprintf(pa, "%d", p);
temp += pa;
temp += "->";
}
result.push_back(temp.substr(0, temp.size()-2));
return;
}
if (root->left != NULL) {
DFS(root->left, path, result);
path.pop_back();
}
if (root->right != NULL) {
DFS(root->right, path, result);
path.pop_back();
}
}
};

lintcode-480-二叉树的所有路径的更多相关文章

  1. 算法 dfs 二叉树的所有路径

    480. 二叉树的所有路径 给一棵二叉树,找出从根节点到叶子节点的所有路径. Example 样例 1: 输入:{1,2,3,#,5} 输出:["1->2->5",&q ...

  2. lintcode:二叉树的所有路径

    二叉树的所有路径 给一棵二叉树,找出从根节点到叶子节点的所有路径. 样例 给出下面这棵二叉树: 1 / \ 2 3 \ 5 所有根到叶子的路径为: [ "1->2->5" ...

  3. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  4. DS树+图综合练习--二叉树之最大路径

    题目描述 给定一颗二叉树的逻辑结构(先序遍历的结果,空树用字符‘0’表示,例如AB0C00D00),建立该二叉树的二叉链式存储结构 二叉树的每个结点都有一个权值,从根结点到每个叶子结点将形成一条路径, ...

  5. LintCode2016年算法比赛----二叉树的所有路径

    二叉树的所有路径 题目描述 给定一棵二叉树,找从根节点到叶子节点的所有路径 样例 给出下面这课二叉树: 1 / \ 2 3 \ 5 所有根到叶子的路径为: [ "1->2->5& ...

  6. [LeetCode] 124. Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  7. 75.Binary Tree Maximum Path Sum(二叉树的最大路径和)

    Level:   Hard 题目描述: Given a non-empty binary tree, find the maximum path sum. For this problem, a pa ...

  8. Java实现 LeetCode 257 二叉树的所有路径

    257. 二叉树的所有路径 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2 ...

  9. 【二叉树-所有路经系列(根->叶子)】二叉树的所有路径、路径总和 II、路径总和、求根到叶子节点数字之和(DFS)

    总述 全部用DFS来做 重点一:参数的设置:为Root,路径字符串,路径List集合. 重点二:步骤: 1 节点为null 2 所有节点的操作 3 叶子结点的操作 4 非叶节点的操作 题目257. 二 ...

  10. [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 ...

随机推荐

  1. 由object元素引出的事件注册问题和层级显示问题

    项目有一个双击监控视频全屏的需求,视频播放使用的是IE下的ActiveX控件,web页面中使用HTML嵌入对象元素object.预期方案如下: 1.在开发ActiveX控件时加入双击事件. 2.通过d ...

  2. php图片上传存储源码,可实现预览

    <?php header("content-Type: text/html; charset=gb2312"); $uptypes=array('image/jpg', // ...

  3. Business Unit Helper

    using System; using System.Linq; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Sy ...

  4. Learning notes | Data Analysis: 1.1 data evaluation

    | Data Evaluation | - Use Shift + Enter or Shift + Return to run the upper box so as to make it disp ...

  5. 关于typedef在struct使用上的一些问题

    typedef struct lnode{ int data; struct lnode next; }lnode,linklist; 第一行的lnode是结构体名,最后一行的lnode是由typed ...

  6. 解决 vboxdrv.sh: failed: Cannot change group vboxusers for device /dev/vboxdrv.

    来自:https://blog.csdn.net/su_cicada/article/details/86773043 virtualbox 报错 ,看提示让执行以下 sudo /sbin/vboxc ...

  7. angularjs与vue循环数组对象是区别

    一直都觉得angularjs和vue是想类似的,今天在限制加载的数据条数时发现 其不同,话不多说,直接看代码: 1.angularjs <li ng-repeat="item in d ...

  8. scala(9) Monad

    一个单子(Monad)说白了不过就是自函子范畴上的一个幺半群而已.这句话涉及到了几个概念:单子(Monad),自函子(Endo-Functor),幺半群(Monoid),范畴(category). 范 ...

  9. 2017-2018-1 20155318《信息安全技术》实验二——Windows口令破解

    2017-2018-1 20155318<信息安全技术>实验二--Windows口令破解 一.实验原理 口令破解方法 口令破解主要有两种方法:字典破解和暴力破解. 字典破解是指通过破解者对 ...

  10. 通知的多线程问题 iOS

    发送通知在子线程,接受也在子线程.如果子线程操作UI,会打印一推日志,告诉我们应该主线程操作.