博客
关于我
LeetCode——Binary Tree Paths
阅读量:795 次
发布时间:2023-01-31

本文共 1223 字,大约阅读时间需要 4 分钟。

Description:

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"]
/** * 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
paths; public List
path; public List
binaryTreePaths(TreeNode root) { paths = new ArrayList
(); path = new ArrayList
(); getAllPath(root); return paths; } public void getAllPath(TreeNode node) { // 1 // / \ // 2 3 ["1->2->5", "1->3"] // \ // 5 if(node == null) { return ; } path.add(node.val); if(node.left==null && node.right==null) { StringBuilder onePath = new StringBuilder(); for(int i=0; i
"); onePath.append(path.get(i)); } paths.add(onePath.toString()); } getAllPath(node.left); getAllPath(node.right); path.remove(path.size() - 1); }}

 

 

转载地址:http://bqgyk.baihongyu.com/

你可能感兴趣的文章
libtorch中python中cuda可以使用,但是是c++环境中不行
查看>>
LibTorch中TensorOptions的使用
查看>>
LibTorch之DataSet数据集处理方法
查看>>
LibTorch之优化器
查看>>
LibTorch之全连接层(torch::nn::Linear)使用
查看>>
LibTorch之图像分类
查看>>
LibTorch之张量操作与线性回归
查看>>
LibTorch之损失函数
查看>>
LibTorch之激活函数层
查看>>
LibTorch之网络层中的卷积层
查看>>
LibTorch之网络模型构建
查看>>
Libtorch在vs中c++相关配置
查看>>
LibTorch实现LeNet
查看>>
LibTorch实现MLP(多层感知机)
查看>>
Libtorch常用代码
查看>>
LibTorch框架学习
查看>>
libtorch组成讲解之ATen、c10、at、csrc
查看>>
libvirt TLS
查看>>
libvirtd tcp 方式远程连接配置步骤
查看>>
libvirt报错处理及解决
查看>>