Leetcode_257_Binary Tree Paths

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pistolove/article/details/49432057

本文是在学习中的总结,欢迎转载但请注明出处: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);
		}
	}
}


Numbering Paths Numbering Paths

09-30

Problem DescriptionrnProblems that process input and generate a simple ``yes'' or ``no'' answer are called decision problems. One class of decision problems, the NP-complete problems, are not amenable to general efficient solutions. Other problems may be simple as decision problems, but enumerating all possible ``yes'' answers may be very difficult (or at least time-consuming). rnrnThis problem involves determining the number of routes available to an emergency vehicle operating in a city of one-way streets.rnrnGiven the intersections connected by one-way streets in a city, you are to write a program that determines the number of different routes between each intersection. A route is a sequence of one-way streets connecting two intersections. rnrnIntersections are identified by non-negative integers. A one-way street is specified by a pair of intersections. For example, j k indicates that there is a one-way street from intersection j to intersection k. Note that two-way streets can be modeled by specifying two one-way streets: j k and k j . rnrnConsider a city of four intersections connected by the following one-way streets: rnrn0 1rn0 2rn1 2rn2 3rnrnThere is one route from intersection 0 to 1, two routes from 0 to 2 (the routes are 0-1-2 and 0-2 ), two routes from 0 to 3, one route from 1 to 2, one route from 1 to 3, one route from 2 to 3, and no other routes. rnIt is possible for an infinite number of different routes to exist. For example if the intersections above are augmented by the street , there is still only one route from 0 to 1, but there are infinitely many different routes from 0 to 2. This is because the street from 2 to 3 and back to 2 can be repeated yielding a different sequence of streets and hence a different route. Thus the route 0-2-3-2-3-2 is a different route than 0-2-3-2 . rn rnrnInputrnThe input is a sequence of city specifications. Each specification begins with the number of one-way streets in the city followed by that many one-way streets given as pairs of intersections. Each pair j k represents a one-way street from intersection j to intersection k. In all cities, intersections are numbered sequentially from 0 to the ``largest'' intersection. All integers in the input are separated by whitespace. The input is terminated by end-of-file. rnrnThere will never be a one-way street from an intersection to itself. No city will have more than 30 intersections.rn rnrnOutputrnFor each city specification, a square matrix of the number of different routes from intersection j to intersection k is printed. If the matrix is denoted M, then M[j][k] is the number of different routes from intersection j to intersection k. The matrix M should be printed in row-major order, one row per line. Each matrix should be preceded by the string ``matrix for city k'' (with k appropriately instantiated, beginning with 0). rnrnIf there are an infinite number of different paths between two intersections a -1 should be printed. DO NOT worry about justifying and aligning the output of each matrix. All entries in a row should be separated by whitespace. rn rnrnSample Inputrn7 0 1 0 2 0 4 2 4 2 3 3 1 4 3rn5 rn0 2 rn0 1 1 5 2 5 2 1rn9rn0 1 0 2 0 3rn0 4 1 4 2 1rn2 0rn3 0rn3 1rn rnrnSample Outputrnmatrix for city 0rn 0 4 1 3 2rn 0 0 0 0 0rn 0 2 0 2 1rn 0 1 0 0 0rn 0 1 0 1 0rnmatrix for city 1rn 0 2 1 0 0 3rn 0 0 0 0 0 1rn 0 1 0 0 0 2rn 0 0 0 0 0 0rn 0 0 0 0 0 0rn 0 0 0 0 0 0rnmatrix for city 2rn -1 -1 -1 -1 -1rn 0 0 0 0 1rn -1 -1 -1 -1 -1rn -1 -1 -1 -1 -1rn 0 0 0 0 0

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试