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. 剑指offer 面试16题

    面试16题: 题目:数值的整数次方 题:实现函数double Power(double base, int exponent),求base的exponent次方.不得使用库函数,同时不需要考虑大数问题 ...

  2. GCE 创建一个Linux VM

    sudo yum install wget 安装Java sudo wget --no-check-certificate --no-cookies --header "Cookie: or ...

  3. git全局忽略

    今天在提交一个本地新建的build文件夹内的文件时,sourceTree始终无法找到该文件夹下的任何内容,但是项目中的.gitignore中并没有写/build/配置,于是开始了填坑之路! 首先从表象 ...

  4. iMX6 yocto平台QT交叉编译环境搭建

    转:https://blog.csdn.net/morixinguan/article/details/79351909 . /opt/fsl-imx-fb/4.9.11-1.0.0/environm ...

  5. 释放Linux系统缓存

    清理Linux缓存使用下面的命令 sync; echo 3 > /proc/sys/vm/drop_caches 需求与原理 下面介绍buffer与cache的差别: A buffer is s ...

  6. nodejs异步调用async

    犹豫nodejs是异步编程模型,有一些在同步编程中很容易做到的事情,现在却变得很麻烦,async的流程控制就是为了简化这些操作var async = require('async'); 1.serie ...

  7. 使用Java实现八种基本排序

    插入排序.选择排序.冒泡排序.希尔排序.堆排序.快速排序.归并排序.基数排序 import java.util.ArrayList; import java.util.List; /** * 排序算法 ...

  8. CentOS 6.5下Redmine的安装配置

    首先引用百度介绍下redmine: Redmine是用Ruby开发的基于web的项目管理软件,是用ROR框架开发的一套跨平台项目管理系统,据说是源于Basecamp的ror版而来,支持多种数据库,有不 ...

  9. Vue.js学习笔记 第八篇 组件

    全局注册组件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...

  10. Python 数值类型

    1.数值类型分为整形(二进制(0b),八进制(0o),十进制,十六进制(0x) ),浮点型,long,complex(复合行) 当我们说十进制数的时候,是逢10进1,就是说到达10的时候就要向前一位进 ...