257:二叉树的所有路径(未完成)
This commit is contained in:
parent
7e340e2f4e
commit
e2c1b69106
65
src/main/java/leetcode/editor/cn/BinaryTreePaths.java
Normal file
65
src/main/java/leetcode/editor/cn/BinaryTreePaths.java
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
//给定一个二叉树,返回所有从根节点到叶子节点的路径。
|
||||||
|
//
|
||||||
|
// 说明: 叶子节点是指没有子节点的节点。
|
||||||
|
//
|
||||||
|
// 示例:
|
||||||
|
//
|
||||||
|
// 输入:
|
||||||
|
//
|
||||||
|
// 1
|
||||||
|
// / \
|
||||||
|
//2 3
|
||||||
|
// \
|
||||||
|
// 5
|
||||||
|
//
|
||||||
|
//输出: ["1->2->5", "1->3"]
|
||||||
|
//
|
||||||
|
//解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3
|
||||||
|
// Related Topics 树 深度优先搜索 字符串 二叉树
|
||||||
|
// 👍 540 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import com.code.leet.entiy.TreeNode;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
//257:二叉树的所有路径
|
||||||
|
public class BinaryTreePaths{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new BinaryTreePaths().new Solution();
|
||||||
|
}
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* public class TreeNode {
|
||||||
|
* int val;
|
||||||
|
* TreeNode left;
|
||||||
|
* TreeNode right;
|
||||||
|
* TreeNode() {}
|
||||||
|
* TreeNode(int val) { this.val = val; }
|
||||||
|
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||||
|
* this.val = val;
|
||||||
|
* this.left = left;
|
||||||
|
* this.right = right;
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
class Solution {
|
||||||
|
public List<String> binaryTreePaths(TreeNode root) {
|
||||||
|
if(root==null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
if(root.left!=null){}
|
||||||
|
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
18
src/main/java/leetcode/editor/cn/BinaryTreePaths.md
Normal file
18
src/main/java/leetcode/editor/cn/BinaryTreePaths.md
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<p>给定一个二叉树,返回所有从根节点到叶子节点的路径。</p>
|
||||||
|
|
||||||
|
<p><strong>说明:</strong> 叶子节点是指没有子节点的节点。</p>
|
||||||
|
|
||||||
|
<p><strong>示例:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>
|
||||||
|
|
||||||
|
1
|
||||||
|
/ \
|
||||||
|
2 3
|
||||||
|
\
|
||||||
|
5
|
||||||
|
|
||||||
|
<strong>输出:</strong> ["1->2->5", "1->3"]
|
||||||
|
|
||||||
|
<strong>解释:</strong> 所有根节点到叶子节点的路径为: 1->2->5, 1->3</pre>
|
||||||
|
<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>字符串</li><li>二叉树</li></div></div>\n<div><li>👍 540</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user