- 浏览: 184656 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 268Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 271You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 388Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 378Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 503Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 567Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 481Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 666Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 472The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 432Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 582Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 589Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 428All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 903Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 933Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 606Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 690Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 855Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 788You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 721For a undirected graph with tre ...
相关推荐
leetcode 树节点二叉树路径 给定一棵二叉树,返回所有从根到...binaryTreePaths ( TreeNode root ) { List< String > result = new ArrayList<> (); if (root == null ){ return result; } dfs(root, " " ,resul
为了实现这个功能,代码定义了一个名为`Solution`的类,该类包含两个成员函数:一个私有的辅助函数`__pathRoot2Leaves`和一个公共的主函数`binaryTreePaths`。 1. `__pathRoot2Leaves(TreeNode* node, string& s)`...
在`binaryTreePaths`主函数中,我们初始化路径字符串`str`为空,结果列表`res`也为空。然后,我们调用`dfs`函数,从根节点开始遍历,最后返回所有找到的路径。 这个解决方案的时间复杂度是O(N),其中N是二叉树的...
题目二叉树所有路径题解* Definition for a binary tree node.* function TreeNode(val) {* @para
26. **Binary Tree Paths**:找到二叉树的所有根到叶子的路径。使用DFS,记录路径并返回。 27. **Remove Linked List Elements**:移除链表中的特定元素。可以使用迭代方法,同时保持指向下一个非目标节点的引用。 ...
Binary Tree Paths **知识点:** - **问题描述:** - 给定一棵二叉树,返回所有从根节点到叶子节点的路径。 - **解决方案分析:** - **深度优先搜索(DFS):** - 从根节点开始 DFS。 - 在叶子节点处记录...
- **Convert Sorted List/Array to Binary Search Tree**:将有序列表或数组转换为二叉搜索树。 - **Path Sum II**:寻找所有从根节点到叶节点的路径,其路径和等于给定的目标值。 - **Flatten Binary Tree to ...
29. Convert Sorted Array to Binary Search Tree:将有序数组转换为高度平衡的二叉搜索树。 30. Convert Sorted List to Binary Search Tree:将有序链表转换为高度平衡的二叉搜索树。 31. Binary Tree Maximum ...
29. Convert Sorted Array to Balanced Binary Search Tree:将有序数组转换成平衡的二叉搜索树。 30. Convert Sorted List to Balanced Binary Search Tree:将有序链表转换成平衡的二叉搜索树。 31. Binary 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.
- 例如,验证二叉搜索树(Validate Binary Search Tree)需要判断给定的二叉树是否符合二叉搜索树的定义。 - 计算二叉树的最大深度(Maximum Depth of Binary Tree)考察递归的使用。 - 平衡二叉树(Balanced Binary...
6. BinaryTree(二叉树): 二叉树是每个节点最多有两个子节点的树数据结构,常用于组织数据,以便进行快速查找、插入和删除。文件中提到的二叉树相关题目包括: - Validate Binary Search Tree(验证二叉搜索树) ...
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 ...
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. //...
- **二叉树的层序遍历(Binary Tree Level Order Traversal)**: 给定一棵二叉树,返回其层序遍历的结果。 - **对称树(Symmetric Tree)**: 判断一个二叉树是否是对称的。 - **相同的树(Same Tree)**: 判断两个二叉树...
5. **二叉树与图**:二叉树题目如"Binary Tree Inorder Traversal"(二叉树的中序遍历)和"Lowest Common Ancestor of a Binary Tree"(二叉树的最近公共祖先),图题目如"Shortest Path in Bidirectional Graph"...