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

边栏推荐
- leetcode刷题:二叉树15(找树左下角的值)
- Is it safe for CICC fortune to open an account online?
- Debezium series: PostgreSQL loads the correct last submission LSN from the offset
- Go language | 01 wsl+vscode environment construction pit avoidance Guide
- 通配符选择器
- Do you know several assertion methods commonly used by JMeter?
- Is it safe for Anxin securities to open an account online?
- Force buckle 1200 Minimum absolute difference
- Common operators and operator priority
- Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
猜你喜欢

微信小程序正则表达式提取链接

Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder

How to apply smart contracts more wisely in 2022?

14. Users, groups, and permissions (14)

leetcode刷题:二叉树10(完全二叉树的节点个数)

秋招字节面试官问你还有什么问题?其实你已经踩雷了

leetcode刷题:二叉树14(左叶子之和)

js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)

解决php无法将string转换为json的办法

Go language | 03 array, pointer, slice usage
随机推荐
多分支结构
c——顺序结构
sun. misc. Base64encoder error reporting solution [easy to understand]
Webuploader file upload drag upload progress monitoring type control upload result monitoring control
再忙不能忘安全
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
Go language learning tutorial (16)
Tasks in GStreamer
Multi branch structure
微信小程序正则表达式提取链接
四万字长文说operator new & operator delete
C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
Where is the operation of new bonds? Is it safer and more reliable to open an account
股票开户哪里好?网上客户经理开户安全吗
How to retrieve the root password of MySQL if you forget it
Using repositoryprovider to simplify the value passing of parent-child components
Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
城链科技数字化创新战略峰会圆满召开
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
Go language | 03 array, pointer, slice usage