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

边栏推荐
- C application interface development foundation - form control (5) - grouping control
- 深度學習 卷積神經網絡(CNN)基礎
- 股票开户哪里好?网上客户经理开户安全吗
- How to safely and quickly migrate from CentOS to openeuler
- ACM getting started Day1
- Fundamentals of deep learning convolutional neural network (CNN)
- - Oui. Net Distributed Transaction and Landing Solution
- How to retrieve the root password of MySQL if you forget it
- third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
- 什么是pyc文件
猜你喜欢

40000 word Wenshuo operator new & operator delete

Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022

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

Zero cloud new UI design

leetcode刷题:二叉树18(最大二叉树)

Fundamentals of deep learning convolutional neural network (CNN)

【无标题】

.Net分布式事務及落地解决方案

.Net分布式事务及落地解决方案

js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
随机推荐
[C language] merge sort
【obs】libobs-winrt :CreateDispatcherQueueController
手机股票开户安全吗?靠不靠谱啊?
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
Database logic processing function
炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗
Concept and syntax of function
成功入职百度月薪35K,2022Android开发面试解答
[untitled]
Do you know several assertion methods commonly used by JMeter?
ACM getting started Day1
Base du réseau neuronal de convolution d'apprentissage profond (CNN)
MySql的root密码忘记该怎么找回
Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
c——顺序结构
Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
Add data to excel small and medium-sized cases through poi
id选择器和类选择器的区别