当前位置:网站首页>Leetcode skimming: binary tree 12 (all paths of binary tree)
Leetcode skimming: binary tree 12 (all paths of binary tree)
2022-07-05 20:03:00 【Taotao can't learn English】
257. All paths of binary tree
Given a binary tree , Return all paths from the root node to the leaf node .
explain : A leaf node is a node that has no children .
Example :
Obviously, the first order traversal of this problem is more comfortable . Use recursion to find the way a little bit .
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>();
// The first sequence traversal
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;
// }
// The first sequence traversal
path += root.val + "->";
// If there is only one direction left Route selection
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);
}
}
边栏推荐
- js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
- sun. misc. Base64encoder error reporting solution [easy to understand]
- Postman core function analysis - parameterization and test report
- Where is the operation of new bonds? Is it safer and more reliable to open an account
- 中金财富在网上开户安全吗?
- Is the education of caiqiantang reliable and safe?
- Parler de threadlocal insecurerandom
- Tasks in GStreamer
- MySql的root密码忘记该怎么找回
- Go language learning tutorial (16)
猜你喜欢
Add data to excel small and medium-sized cases through poi
Postman core function analysis - parameterization and test report
Securerandom things | true and false random numbers
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
Do you know several assertion methods commonly used by JMeter?
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer
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
Go language | 02 for loop and the use of common functions
leetcode刷题:二叉树14(左叶子之和)
How to safely and quickly migrate from CentOS to openeuler
随机推荐
leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
Wildcard selector
解决php无法将string转换为json的办法
Where is the operation of new bonds? Is it safer and more reliable to open an account
Let's talk about threadlocalinsecurerandom
Is it safe for Galaxy Securities to open an account online?
Recommended collection, my Tencent Android interview experience sharing
The city chain technology Digital Innovation Strategy Summit was successfully held
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
浮动元素与父级、兄弟盒子的关系
深度学习 卷积神经网络(CNN)基础
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
挖财钱堂教育靠谱安全吗?
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
40000 word Wenshuo operator new & operator delete
Go language learning tutorial (XV)
Postman core function analysis - parameterization and test report
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验