LeetCode OJ:Binary Tree Paths(二叉树路径)
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(二叉树路径)的更多相关文章
- [LeetCode] 257. 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 ...
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- [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 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- LeetCode 257. 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 ...
- 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(53)-Binary Tree Paths
题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...
随机推荐
- 002-基本业务搭建【日志,工具类dbutils,dbcp等使用】
一.需求分析 1.1.概述 1.用户进入“客户管理”,通过列表方式查看用户: 2.客户名称,模糊查询用户列表 3.客户名称,可查看客户详细信息 4.新增.编辑.删除功能等 二.系统设计 需要对原始需求 ...
- 如何高效地分析Android_log中的问题?——查看Android源码
在日常解bugs时,需要通过log日志来分析问题,例如查看crash发生时的堆栈信息时,就会有Android的源码的调用,这是就要去查看Android源码. 1.进入Android源码网址查看,例如 ...
- Python函数之—— 装饰器(Day13)
一.什么是装饰器 顾名思义,装饰器指为其他函数添加新功能 装饰器定义:本质就是函数,功能是为其他函数添加新功能 二.装饰器需要遵循的原则 1.不修改被装饰函数的源代码(开放封闭原则) 2.为被装饰函数 ...
- Python之函数2 嵌套,作用域和闭包(Day12)
一.函数对象 1.函数是第一类对象,即函数可以当做数据传递 1.1 可以被引用 1.2 可以当做参数传递 1.3 返回值可以是函数 1.4 可以当做容器类型的元素 二.函数的嵌套 1.函数嵌套的调用: ...
- jQuery:自学笔记(2)——jQuery选择器
jQuery:自学笔记(2)——jQuery选择器 基本选择器 说明 jQuery的基本选择器与CSS的选择器相似: 实例 标签选择器 //使用标签选择器更改字体大小 $(div).css('font ...
- Linux基本命令 关机命令
linux下常用的关机命令有:shutdown.halt.poweroff.init:重启命令有:reboot.下面本文就主要介绍一些常用的关机命令以及各种关机命令之间的区别和具体用法. 首先来看一下 ...
- The Collections Module内建collections集合模块
https://www.bilibili.com/video/av17396749/?p=12 Python函数式编程中的迭代器,生成器详解 课程内容 1.iterators are objects ...
- Linux 安装扩展yum源
Linux 安装扩展yum源 下载rpm扩展:http://rpmfind.net/linux/epel/6/x86_64/epel-release-6-8.noarch.rpm CentOS/RHE ...
- ACM训练小结-2018年6月15日
今天题目情况如下:A题:给出若干条边的边长,问这些边按顺序能否组成一个凸多边形,并求出这个多边形的最小包含圆.答题情况:无思路.正解(某种):第一问很简单.对第二问,如果R大于可行的最小R,那么按照放 ...
- Android电容屏(一)【转】
本文转载自:http://blog.csdn.net/xubin341719/article/details/7820492 关键词:Android 电容屏 tp ITO 平台信息:内核:linu ...