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:
vector<string> binaryTreePaths(TreeNode* root) {
ret.clear();
string s = "";
if(root == NULL) return ret;
dfs(root, s);
for(int i = ; i < ret.size(); ++i){
ret[i].erase(ret[i].begin(), ret[i].begin() + );
}
return ret;
} void dfs(TreeNode * root, string s)
{
stringstream ss;
ss << "->" << root->val;
s += ss.str();
if(root->left == NULL && root->right == NULL){
ret.push_back(s);
return;
}
if(root->left){
dfs(root->left, s);
}
if(root->right){
dfs(root->right, s);
}
}
private:
vector<string> ret;
};

java版本的如下所示,和c++的相比还是要简单很多的,因为处理字符串的函数用起来比较方便的原因,代码如下:

 public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> ret = new ArrayList<String>();
String str = new String();
if(root == null)
return ret;
dfs(root, str, ret);
return ret;
}
public void dfs(TreeNode root, String path, List<String> ret){
if(root.left != null){
path = path + "->" + root.val;
dfs(root.left, path, ret);
path = path.substring(0, path.lastIndexOf("->")); //引用其他的
} //还是要继续使用的,截断即可
if(root.right != null){
path = path + "->" + root.val;
dfs(root.right, path, ret);
path = path.substring(0, path.lastIndexOf("->"));
}
if(root.left == null && root.right == null){
path = path + "->" + root.val;
ret.add(path.substring(2));
path = path.substring(0,path.lastIndexOf("->"));
return;
}
}
}

LeetCode OJ:Binary Tree Paths(二叉树路径)的更多相关文章

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

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

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

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

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

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

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

  5. Leetcode 257 Binary Tree Paths 二叉树 DFS

    找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...

  6. LeetCode 257. Binary Tree Paths (二叉树路径)

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

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

  8. 257 Binary Tree Paths 二叉树的所有路径

    给定一个二叉树,返回从根节点到叶节点的所有路径.例如,给定以下二叉树:   1 /   \2     3 \  5所有根到叶路径是:["1->2->5", " ...

  9. 【easy】257. Binary Tree Paths 二叉树找到所有路径

    http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...

  10. LeetCode(53)-Binary Tree Paths

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

随机推荐

  1. discuz论坛用户资料采集器

    discuz论坛用户资料采集器, 自动采集用户信息!

  2. .net全部版本的官方下载地址

    https://technet.microsoft.com/zh-cn/5a4x27ek

  3. LATEX教程(一)

    第一个文档 打开WinEdt,建立一个新文档,将以下内容复制进入文档中,保存,保存类型选择为UTF-8. \documentclass{article} \begin{document} hello, ...

  4. Mybatis使用pageHelper步骤

    1.在pom.xml中添加如下依赖: <dependency> <groupId>com.github.pagehelper</groupId> <artif ...

  5. 3.7 基于51单片机+MC20的路径显示【使用STC15W内核】

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...

  6. 返回泛型集合的SqlDBHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Entity; ...

  7. jsp导出

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  8. 配置树莓派3和局域网NTP服务器实现内网时间校准

    一.配置局域网NTP服务器 1.安装ntp-4.2.8p5-win32-setup.exe 下载地址:https://www.meinbergglobal.com/english/sw/ntp.htm ...

  9. mysql多表查询原理

    转:https://www.cnblogs.com/Toolo/p/3634563.html MySQL的多表查询(笛卡尔积原理)   先确定数据要用到哪些表. 将多个表先通过笛卡尔积变成一个表. 然 ...

  10. 登陆weblogic后页面控制台卡主

    输入http://localhost:7001/console进入控制页面,能登陆进去,但是登陆进去后页面就马上卡死,可以看到页面头部,其余都显示不出来. 重启后启动访问,能够正常进入,关闭weblo ...