本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

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

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

  2. LintCode Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...

  3. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  4. &lt;LeetCode OJ&gt; 257. Binary Tree Paths

    257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...

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

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

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

  7. leetcode : Binary Tree Paths

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

  8. Leetcode 257. Binary Tree Paths

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

  9. Binary Tree Paths

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

随机推荐

  1. 分析MapReduce执行过程+统计单词数例子

    MapReduce 运行的时候,会通过 Mapper 运行的任务读取 HDFS 中的数据文件,然后调用自己的方法,处理数据,最后输出.Reducer 任务会接收 Mapper 任务输出的数据,作为自己 ...

  2. Dynamics CRM Trace Reader for Microsoft Dynamics CRM

    CRM中抓取日志的视窗工作叫做Diagnastics Tools For Dyanmics CRM,这个工具我们只是作为一个开关来用就不做多介绍了,日志生成后是个文本文档可读性是很差的,那就需要个视窗 ...

  3. Android View框架总结(九)KeyEvent事件分发机制

    请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52335094 本篇开始分析按键消息事件分发(PS:本篇文章中源码均是 ...

  4. 海量并发的无锁编程 (lock free programming)

    最近在做在线架构的实现,在线架构和离线架构近线架构最大的区别是服务质量(SLA,Service Level Agreement,SLA 99.99代表10K的请求最多一次失败或者超时)和延时.而离线架 ...

  5. HTML5中 HTML列表/块/布局 韩俊强的博客

    从简单到复杂HTML5详解:每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 1.HTML列表 1.有序 2.无序 3.有序star属性 4.有序无序列表 代码: ...

  6. UNIX网络编程——使用select 实现套接字I/O超时

    下面程序包含read_timeout.write_timeout.accept_timeout.connect_timeout 四个函数封装: /* read_timeout - 读超时检测函数,不含 ...

  7. python进行md5加密

    代码函数 import hashlib def md5(str): m = hashlib.md5() m.update(str) return m.hexdigest() f = open('idf ...

  8. Hadoop:Hadoop基本命令

    http://blog.csdn.net/pipisorry/article/details/51223877 常用命令 启用hadoop start-dfs.sh start-hbase.sh 停止 ...

  9. Android的Notification的简介-android学习之旅(四十一)

    Notification简介 Notification位于手机饿最上面,用于显示手机的各种信息,包括网络状态,电池状态,时间等. 属性方法介绍 代码示例 package peng.liu.test; ...

  10. 头部——MimeHeaders

    http协议的请求头部更像一个键值对,例如Content-Length : 123,前面为键后面为值,表示文本长度为123.对于若干个头部在请求对象中被封装成MimeHeaders对象,MimeHea ...