当前位置:网站首页>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);
}
}

边栏推荐
- ICTCLAS word Lucene 4.9 binding
- DP: tree DP
- Parler de threadlocal insecurerandom
- What is PyC file
- Leetcode brush questions: binary tree 11 (balanced binary tree)
- Add data to excel small and medium-sized cases through poi
- 如何安全快速地从 Centos迁移到openEuler
- Go language learning tutorial (XV)
- Summer Challenge harmonyos - realize message notification function
- 处理文件和目录名
猜你喜欢

leetcode刷题:二叉树13(相同的树)

Leetcode brush question: binary tree 14 (sum of left leaves)
![[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp](/img/32/738df44b6005fd84b4a9037464e61e.jpg)
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp

Webuploader file upload drag upload progress monitoring type control upload result monitoring control

Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)

淺淺的談一下ThreadLocalInsecureRandom

深度学习 卷积神经网络(CNN)基础

leetcode刷题:二叉树16(路径总和)

【无标题】

建立自己的网站(16)
随机推荐
中金财富在网上开户安全吗?
What is PyC file
leetcode刷题:二叉树16(路径总和)
港股将迎“最牛十元店“,名创优品能借IPO突围?
Thread pool parameters and reasonable settings
sun. misc. Base64encoder error reporting solution [easy to understand]
四万字长文说operator new & operator delete
aggregate
Base du réseau neuronal de convolution d'apprentissage profond (CNN)
Flume series: interceptor filtering data
Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
Process file and directory names
Summer Challenge harmonyos - realize message notification function
leetcode刷题:二叉树14(左叶子之和)
leetcode刷题:二叉树11(平衡二叉树)
Bzoj 3747 poi2015 kinoman segment tree
零道云新UI设计中
Recommended collection, my Tencent Android interview experience sharing
c语言oj得pe,ACM入门之OJ~
使用 RepositoryProvider简化父子组件的传值