当前位置:网站首页>leetcode刷题:二叉树12(二叉树的所有路径)
leetcode刷题:二叉树12(二叉树的所有路径)
2022-07-05 19:52:00 【涛涛英语学不进去】
257. 二叉树的所有路径
给定一个二叉树,返回所有从根节点到叶子节点的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
显然这题先序遍历比较舒服。使用递归一点点找路。
package com.programmercarl.tree;
import com.programmercarl.util.GenerateTreeNode;
import java.util.ArrayList;
import java.util.List;
/** * @ClassName BinaryTreePaths * @Descriotion TODO * @Author nitaotao * @Date 2022/7/4 18:16 * @Version 1.0 **/
public class BinaryTreePaths {
public static List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<String>();
//先序遍历
getPath(root, result, "");
return result;
}
public static void getPath(TreeNode root, List<String> result, String path) {
// if (root == null) {
// result.add(path.substring(0, path.length() - 2));
// return;
// }
//先序遍历
path += root.val + "->";
//如果接下来只剩一个方向了 选路
if (root.left == null && root.right != null) {
getPath(root.right, result, path);
} else if (root.left != null && root.right == null) {
getPath(root.left, result, path);
} else if (root.left == null && root.right == null){
result.add(path.substring(0, path.length() - 2));
return;
}else{
getPath(root.left, result, path);
getPath(root.right, result, path);
}
}
public static void main(String[] args) {
TreeNode node = GenerateTreeNode.generateTreeNode("[1]");
binaryTreePaths(node);
}
}
边栏推荐
- 【obs】libobs-winrt :CreateDispatcherQueueController
- 不愧是大佬,字节大牛耗时八个月又一力作
- Recommended collection, my Tencent Android interview experience sharing
- How about testing outsourcing companies?
- 多分支结构
- CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
- acm入门day1
- Android interview, Android audio and video development
- 打新债在哪里操作开户是更安全可靠的呢
- 使用 RepositoryProvider简化父子组件的传值
猜你喜欢
redis集群模拟消息队列
安卓面试宝典,2022Android面试笔试总结
软件测试工程师是做什么的?待遇前景怎么样?
Using repositoryprovider to simplify the value passing of parent-child components
What do software test engineers do? How about the prospect of treatment?
Build your own website (16)
Webuploader file upload drag upload progress monitoring type control upload result monitoring control
Summer Challenge database Xueba notes, quick review of exams / interviews~
What does software testing do? What are the requirements for learning?
Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
随机推荐
XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
Common operators and operator priority
司空见惯 - 英雄扫雷鼠
城链科技数字化创新战略峰会圆满召开
建立自己的网站(16)
Relationship between floating elements and parent and brother boxes
太牛了,看这篇足矣了
Is it safe for Guosen Securities to open an account online?
openh264解码数据流向分析
What do software test engineers do? How about the prospect of treatment?
ACM getting started Day1
测试外包公司怎么样?
Is it safe to open a mobile stock account? Is it reliable?
Postman核心功能解析-参数化和测试报告
id选择器和类选择器的区别
Jvmrandom cannot set seeds | problem tracing | source code tracing
众昂矿业:2022年全球萤石行业市场供给现状分析
leetcode刷题:二叉树16(路径总和)
JMeter 常用的几种断言方法,你会了吗?