`
hcx2013
  • 浏览: 83336 次
社区版块
存档分类
最新评论

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.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
	List<String> res = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        if (root != null) {
        	findPaths(root, String.valueOf(root.val));
        }
        return res;
    }
	private void findPaths(TreeNode root, String valueOf) {
		// TODO Auto-generated method stub
		if (root.left == null && root.right == null) {
			res.add(valueOf);
		}
		if (root.left != null) {
			findPaths(root.left, valueOf + "->" + root.left.val);
		}
		if (root.right != null) {
			findPaths(root.right, valueOf + "->" + root.right.val);
		}
	}
}
 
分享到:
评论

相关推荐

    leetcodetreenode-binary-tree-paths:二叉树路径

    leetcode 树节点二叉树路径 给定一棵二叉树,返回所有从根到...binaryTreePaths ( TreeNode root ) { List&lt; String &gt; result = new ArrayList&lt;&gt; (); if (root == null ){ return result; } dfs(root, " " ,resul

    MengZhaoFly#sword-byteDance-fe#二叉树的所有路径(binary-tree-paths)1

    题目二叉树所有路径题解* Definition for a binary tree node.* function TreeNode(val) {* @para

    cpp-算法精粹

    Construct Binary Tree from Inorder and Postorder Traversal 二叉查找树 Unique Binary Search Trees Unique Binary Search Trees II Validate Binary Search Tree Convert Sorted Array to Binary Search Tree ...

    BST_Paths.rar_them

    This is a C++ code. Given a set of numbers, the task is to build a binary search tree, and to certain numbers, if they are searching in the tree, and say which way to reach them.

    leetcode分类-LeetCode:力码

    leetcode 分类 LeetCode Progress 128/154 Other Solutions C++,有详细思路解释 python,部分有解释 Java,部分有解释 ...norvig神牛Python代码写的很飘逸,果然是有LISP内功的人!...Paths ...Binary Tree Binar

    EIT - The Internal Extent Formula for Compacted Tries-计算机科学

    Università degli Studi di Milano, ItalyAbstractIt is well known [Knu97, pages 399–400] that in a binary tree the external path length minus the internal path length is exactly 2n � 2, where n is ...

    算法导论_英文第三版

    12.2 Querying a binary search tree 289 12.3 Insertion and deletion 294 ? 12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 ...

    【leetcode】【medium】113. Path Sum II

    Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, ...

    javalruleetcode-DataStructures:使用面向对象的方法和单元测试用例在Java中实现各种数据结构

    binary tree and a sum, find all the paths that leads to the sum. //I had two questions: One was the Djikstra's shortest path algorithm and the second was to find the missing element in the array. //...

    Introduction to Algorithms, 3rd edtion

    12.2 Querying a binary search tree 289 12.3 Insertion and deletion 294 12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 13.3...

    算法导论 第三版 英文原版 高清文字版

    12.2 Querying a binary search tree 289 12.3 Insertion and deletion 294 ? 12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 ...

    算法导论-introduction to algrithms 3rd edition

    286 12.2 Querying a binary search tree 289 12.3 Insertion and deletion 294 ? 12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations ...

    lrucacheleetcode-leetcode:leetcode

    binary_tree_paths.py: bst_search.py​​: find_disappeared_numbers.py: frequecy_sort.py: height_checker.py: Jewels_and_stones.py: last_stone_weight.py: Linked_list_cycle.py: long_pressed_name.py...

    算法导论英文版

    l2.2 Querying a binary search tree 2S6 l2.3 Insertion and deletion 261 l2.4 Randoinly built binary search trees 265 13 Red-Black Thees 273 l3.l Properties of red-black trees 273 l3.2 Rotations 277 l...

    leetcode答案-leetcode:leetcode

    Binary Tree 这?也太简单了吧。。一行代码,一个尾递归搞定啊。。 终于想清楚了,leetcode的AC率应该是:在线编辑、肉眼检查,提交的准确率!借助线下debug工具,有何难度可言?丝毫没有模拟在线面试的味道了。。...

    LeetCode — Path Sum III分析及实现方法

    You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must ...

    算法导论--Introduction.to.Algorithms

    12.2 Querying a binary search tree 289 12.3 Insertion and deletion 294 12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 13.3...

    gasstationleetcode-leetcode:LeetcodeOJ解决方案

    加油站 leetcode 【演示记录】 报告 展示 2017/03/06 1.二和,167.二和二 2107/03/06 15.3 总和,16.3 ...总和,11....Paths, ...Paths ...Binary Search Tree, 120.Triangle, 139.Word Break, 152.Maximum Produ

    Data.Structure.Practice.for.Collegiate.Programming.Contests.and.Education

    Chapter 8: Programming by Tree Structure Chapter 9: Applications of Binary Trees Chapter 10: Applications of Classical Trees Section IV: Experiments for Graphs Chapter 11: Applications of Graph ...

Global site tag (gtag.js) - Google Analytics