Leetcode_257_Binary Tree Paths
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/49432057
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"]
思路:
(1)题意为给定一棵树,找出所有从根到叶子节点的路径。
(2)该题实为树的深度优先遍历。本题是使用递归的方法来进行求解的,从根节点开始,若左子树不为空,则遍历左子树,若左子树的左孩子不为空,则遍历左孩子,否则遍历右孩子.....直到遍历完最后一个叶子节点为止。使用非递归算法,则需要设定一个栈来保存左右子树,也很好实现,这里不累赘了。
(3)详情见下方代码。希望本文对你有所帮助。
package leetcode;
import java.util.ArrayList;
import java.util.List;
import leetcode.utils.TreeNode;
public class Binary_Tree_Paths {
public static void main(String[] args) {
TreeNode r = new TreeNode(1);
TreeNode r1 = new TreeNode(2);
TreeNode r2 = new TreeNode(3);
TreeNode r3 = new TreeNode(5);
r.left = r1;
r.right = r2;
r1.right = r3;
binaryTreePaths(r);
}
public static List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<String>();
if (root != null) {
getpath(root, String.valueOf(root.val), result);
}
return result;
}
private static void getpath(TreeNode root, String valueOf,
List<String> result) {
if (root.left == null && root.right == null)
result.add(valueOf);
if (root.left != null) {
getpath(root.left, valueOf + "->" + root.left.val, result);
}
if (root.right != null) {
getpath(root.right, valueOf + "->" + root.right.val, result);
}
}
}
Leetcode_257_Binary Tree Paths的更多相关文章
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- LintCode Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- LeetCode_257. Binary Tree Paths
257. Binary Tree Paths Easy Given a binary tree, return all root-to-leaf paths. Note: A leaf is a no ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 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. For example, given the following binary tree: 1 ...
- Binary Tree Paths
Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...
随机推荐
- paypal的IPN机制
paypal对接时发现有这么一个机制,看起来还不错,起到了防止篡改欺诈行为,保证了通信的安全性,但会增加几次通信.
- Freemarker 浅析
今天分享一下一个模板语言的使用,它就是Freemarker,有点类似与前些日子做Python的Django中的模板语言,其实原理上都是相似的.所以这里就不对那些基础性的语法类的直至进行讲解了,就拿几个 ...
- JAVA之旅(三十三)——TCP传输,互相(伤害)传输,复制文件,上传图片,多并发上传,多并发登录
JAVA之旅(三十三)--TCP传输,互相(伤害)传输,复制文件,上传图片,多并发上传,多并发登录 我们继续网络编程 一.TCP 说完UDP,我们就来说下我们应该重点掌握的TCP了 TCP传输 Soc ...
- Android中三种计时器Timer、CountDownTimer、handler.postDelayed的使用
在android开发中,我们常常需要用到计时器,倒计时多少秒后再执行相应的功能,下面我就分别来讲讲这三种常用的计时的方法. 一.CountDownTimer 该类是个抽象类,如果要使用这个类中的方法, ...
- Sky(dart)语言介绍-android学习之旅(十)
认识dart语言 google于2011年10月10日发布了"dart"语言的"早起预览版",google希望利用这款语言,帮助开发者克服javaScript的 ...
- 学习TensorFlow,线性回归模型
学习TensorFlow,在MNIST数据集上建立softmax回归模型并测试 一.代码 <span style="font-size:18px;">from tens ...
- 【一天一道LeetCode】#342. Power of Four
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Access数据类型和.NET数据类型映射
下表列出了 Microsoft Access 和这些数据类型与 Microsoft.NET Framework 数据类型与 OleDbType 枚举的方式中使用的最常见的数据类型. 访问类型名称 数据 ...
- Android官方命令深入分析之虚拟机
Android SDK包含了一个运行在计算机上的移动设备虚拟机.这个虚拟机可以允许你在没有物理设备的情况下开发和测试Android应用. 键盘命令 虚拟设备按键 对应键盘按键 Home HOME 菜单 ...
- Install and run DB Query Analyzer 6.04 on Microsoft Windows 10
Install and run DB Query Analyzer 6.04 on Microsoft Windows 10 DB Query Analyzer is presented ...