当前位置:网站首页>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);
}
}
边栏推荐
- 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
- Database logic processing function
- How about testing outsourcing companies?
- Let's talk about threadlocalinsecurerandom
- C#应用程序界面开发基础——窗体控制(5)——分组类控件
- third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
- 安信证券在网上开户安全吗?
- id选择器和类选择器的区别
- Do you know several assertion methods commonly used by JMeter?
- 95后阿里P7晒出工资单:狠补了这个,真香...
猜你喜欢
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
ACM getting started Day1
leetcode刷题:二叉树11(平衡二叉树)
[untitled]
Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
.Net分布式事務及落地解决方案
Webuploader file upload drag upload progress monitoring type control upload result monitoring control
Jvmrandom cannot set seeds | problem tracing | source code tracing
如何安全快速地从 Centos迁移到openEuler
Using repositoryprovider to simplify the value passing of parent-child components
随机推荐
Debezium series: modify the source code to support UNIX_ timestamp() as DEFAULT value
Securerandom things | true and false random numbers
常用运算符与运算符优先级
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
2023年深圳市绿色低碳产业扶持计划申报指南
S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
ICTCLAS word Lucene 4.9 binding
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
[C language] three implementations of quick sorting and optimization details
Cocos2d-x项目总结中的一些遇到的问题
Is it safe for Galaxy Securities to open an account online?
Postman core function analysis - parameterization and test report
40000 word Wenshuo operator new & operator delete
Multi branch structure
sun. misc. Base64encoder error reporting solution [easy to understand]
股票开户哪里好?网上客户经理开户安全吗
14. Users, groups, and permissions (14)
手机股票开户安全吗?靠不靠谱啊?
Is it safe for Guosen Securities to open an account online?
处理文件和目录名