当前位置: 首页 > news >正文

LeetCode:257. 二叉树的所有路径

跟着carl学算法,本系列博客仅做个人记录,建议大家都去看carl本人的博客,写的真的很好的!
代码随想录

LeetCode:257. 二叉树的所有路径
给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
叶子节点 是指没有子节点的节点。
示例 1:
在这里插入图片描述
输入:root = [1,2,3,null,5]
输出:[“1->2->5”,“1->3”]
示例 2:
输入:root = [1]
输出:[“1”]

注意这里traversal函数里面path使用的是list,是引用传递的,需要回溯;前序遍历,中左右

	public List<String> binaryTreePaths(TreeNode root) {if (root == null)return new ArrayList<>();// 存放结果List<String> res = new ArrayList<>();// 存放当前的路径List<String> path = new ArrayList<>();traversal(root, path, res);return res;}private void traversal(TreeNode cur, List<String> path, List<String> res) {// 先将当前节点的val放入path中,这里不考虑NPE,非空的时候在调用该方法path.add(cur.val + "");// 如果当前节点是叶子节点if (cur.left == null && cur.right == null) {String temp = String.join("->", path);res.add(temp);}// 左子结点不为空才继续向左if (cur.left != null) {traversal(cur.left, path, res);// 这个remove就是回溯的过程!path.remove(path.size() - 1);}// 右子结点不为空才继续向右if (cur.right != null) {traversal(cur.right, path, res);path.remove(path.size() - 1);}}

和上面解法的区别就是初始的时候就往list里面放cur.val了,这样在traversal方法里面就仅剩:终止条件,单层递归逻辑(分别向左,右遍历),就是一个简单的前序遍历,中左右

	public List<String> binaryTreePaths(TreeNode root) {if (root == null)return new ArrayList<>();// 存放结果List<String> res = new ArrayList<>();// 存放当前的路径List<String> path = new ArrayList<>();path.add(root.val + "");traversal(root, path, res);return res;}private void traversal(TreeNode cur, List<String> path, List<String> res) {// 如果当前节点是叶子节点if (cur.left == null && cur.right == null) {String temp = String.join("->", path);res.add(temp);}// 左子结点不为空才继续向左if (cur.left != null) {path.add(cur.left.val + "");traversal(cur.left, path, res);// 这个remove就是回溯的过程!path.remove(path.size() - 1);}// 右子结点不为空才继续向右if (cur.right != null) {path.add(cur.right.val + "");traversal(cur.right, path, res);path.remove(path.size() - 1);}}

精简版,traversal方法参数中传的是字符串

	public List<String> binaryTreePaths(TreeNode root) {if (root == null)return new ArrayList<>();List<String> res = new ArrayList<>();traversal(root, "", res);return res;}private void traversal(TreeNode cur, String path, List<String> res) {path += cur.val;if (cur.left == null && cur.right == null) {res.add(path);}if (cur.left != null) {traversal(cur.left, path + "->", res);}if (cur.right != null) {traversal(cur.right, path + "->", res);}}
http://www.lryc.cn/news/509483.html

相关文章:

  • RSICV国产芯片之CHV208
  • 理解神经网络
  • Android 之 List 简述
  • 设计模式の中介者发布订阅备忘录模式
  • 云手机群控能用来做什么?
  • fpgafor循环语句使用
  • 【FastAPI】BaseHTTPMiddleware类
  • Solon v3.0.5 发布!(Spring 可以退休了吗?)
  • 网络安全攻防演练中的常见计策
  • SD卡模块布局布线设计
  • Flask-----SQLAlchemy教程
  • STM32 高级 物联网通信之CAN通讯
  • “乡村探索者”:村旅游网站的移动应用开发
  • 前端案例---自定义鼠标右键菜单
  • 浅谈归一化
  • lodash常用函数
  • 触控算法总结
  • 齐次矩阵包含平移和旋转
  • Move AI技术浅析(四):运动跟踪与估计
  • NCR+可变电荷块3——NCB/cell绘图1
  • 数据仓库是什么?数据仓库简介
  • AI的进阶之路:从机器学习到深度学习的演变(二)
  • C++中属性(Attributes)
  • Go语言中的defer,panic,recover 与错误处理
  • (C语言)力扣 904.水果成篮
  • 2024 年12月英语六级CET6听力原文(Lecture部分)
  • CentOS下,离线安装vscode的步骤;
  • ubuntu停止.netcore正在运行程序的方法
  • 机器学习基础 衡量模型性能指标
  • 《OpenCV计算机视觉》-对图片的各种操作(均值、方框、高斯、中值滤波处理)及形态学处理