[LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.
Example
Given the following binary tree:
1
 /   \
2     3
 \
  5
All root-to-leaf paths are:
[
  "1->2->5",
  "1->3"
]
LeetCode上的原题,请参见我之前的博客Binary Tree Paths。
解法一:
class Solution {
public:
    /**
     * @param root the root of the binary tree
     * @return all root-to-leaf paths
     */
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> res;
        if (root) helper(root, "", res);
        return res;
    }
    void helper(TreeNode *node, string out, vector<string> &res) {
        out += to_string(node->val);
        if (!node->left && !node->right) {
            res.push_back(out);
        } else {
            if (node->left) helper(node->left, out + "->", res);
            if (node->right) helper(node->right, out + "->", res);
        }
    }
};
解法二:
class Solution {
public:
    /**
     * @param root the root of the binary tree
     * @return all root-to-leaf paths
     */
    vector<string> binaryTreePaths(TreeNode* root) {
        if (!root) return {};
        if (!root->left && !root->right) return {to_string(root->val)};
        vector<string> left = binaryTreePaths(root->left);
        vector<string> right = binaryTreePaths(root->right);
        left.insert(left.end(), right.begin(), right.end());
        for (auto &a : left) {
            a = to_string(root->val) + "->" + a;
        }
        return left;
    }
};
[LintCode] Binary Tree Paths 二叉树路径的更多相关文章
- [LeetCode] 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二叉树路径
		Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ... 
- [LeetCode] 257. Binary Tree Paths 二叉树路径
		Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ... 
- LintCode Binary Tree Paths
		Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ... 
- 257 Binary Tree Paths 二叉树的所有路径
		给定一个二叉树,返回从根节点到叶节点的所有路径.例如,给定以下二叉树: 1 / \2 3 \ 5所有根到叶路径是:["1->2->5", " ... 
- 【easy】257. Binary Tree Paths 二叉树找到所有路径
		http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ... 
- Leetcode 257 Binary Tree Paths 二叉树 DFS
		找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ... 
- [LintCode] Invert Binary Tree 翻转二叉树
		Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ... 
- 【LeetCode】257. Binary Tree Paths
		Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ... 
随机推荐
- Java 对象序列化(Serialization Object)
			官网文档:https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html 优秀博客: http://www.cnblogs.com/g ... 
- linux内核的组成,王明学learn
			linux内核的组成 一.linux内核源代码目录结构 arch: 包含和硬件体系结构相关的代码, 每种平台占一个相应的目录, 如 i386.ARM.PowerPC.MIPS 等. block:块设备 ... 
- UED
			User Experience Design(用户体验设计),简称UED.UED是进行产品策划的主力之一,他们用自己的知识.经验.设计能力拿出设计方案. UED不只是互联网专家,还是行业专家.能够用自 ... 
- cocos2dx游戏开发——微信打飞机学习笔记(二)——游戏框架
			一.游戏的基本框架: WelcomeScene ——> GameScene ——> GameOverScene || ... 
- AIDL
			在介绍跨程序进程间通信AIDL前,先看一下本程序activity与某个服务是怎么绑定在一起进行交互的. 需求:服务有两个方法.分别是播放音乐与停止播放音乐.该程序的活动要访问这两个方法,在activi ... 
- user-select
			样式详查 http://www.css88.com/book/css/properties/user-interface/user-select.htm 1, user-select: none ... 
- 近实时运算的利器---presto在公司实践
			1.起因 公司hadoop集群里的datanonde和tasktracker节点负载主要集中于晚上到凌晨,平日工作时间负载不是很高.但在工作时间内,公司业务人员有实时查询需求,现在主要 借助于hive ... 
- canvas之2D上下文
			1.填充和描边 (1)fillStyle (2)strokeStyle 2.绘制矩形 (1)fillRect() (2)strokeRect() (3)clearRect() :清除画布上的矩形区 ... 
- DSP using MATLAB 示例Example3.22
			代码: % Discrete-time Signal x2(n) Ts = 0.001; n = -5:1:5; nTs = n*Ts; Fs = 1/Ts; x = exp(-1000*abs(nT ... 
- dubbo服务治理框架设计
			dubbo.JSF作为使用最广泛的服务端框治理架,其设计和实现思想值得进行学习研究. 整个服务管理框架核心的原理基于反射以及socket调用实现,服务管理框架包含服务的注册管理 服务的索引管理以及服务 ... 
