- 浏览: 174011 次
- 性别:
- 来自: 济南
文章分类
最新评论
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"]
给定一个二叉树,输出从根到叶子节点的路径。用递归来完成,代码如下:
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 { public List<String> binaryTreePaths(TreeNode root) { List<String> list = new ArrayList<String>(); if(root == null) return list; getPath(root, String.valueOf(root.val), list); return list; } public void getPath(TreeNode root, String s, List<String> list) { if(root.left == null && root.right == null) list.add(s); if(root.left != null) getPath(root.left, s + "->" + root.left.val, list); if(root.right != null) getPath(root.right, s + "->" + root.right.val, list); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 228Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 230You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 344Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 335Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 454Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 515Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 432Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 623Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 431The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 389Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 521Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 536Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 373All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 866Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 884Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 557Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 603Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 766Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 738You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 631For a undirected graph with tre ...
相关推荐
leetcode 树节点二叉树路径 给定一棵二叉树,返回所有从根到...binaryTreePaths ( TreeNode root ) { List< String > result = new ArrayList<> (); if (root == null ){ return result; } dfs(root, " " ,resul
题目二叉树所有路径题解* Definition for a binary tree node.* function TreeNode(val) {* @para
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 ...
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 Progress 128/154 Other Solutions C++,有详细思路解释 python,部分有解释 Java,部分有解释 ...norvig神牛Python代码写的很飘逸,果然是有LISP内功的人!...Paths ...Binary Tree Binar
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 ...
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, ...
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. //...
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 ...
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 ...
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...
Binary Tree 这?也太简单了吧。。一行代码,一个尾递归搞定啊。。 终于想清楚了,leetcode的AC率应该是:在线编辑、肉眼检查,提交的准确率!借助线下debug工具,有何难度可言?丝毫没有模拟在线面试的味道了。。...
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 ...
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...
加油站 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
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 ...