当前位置:网站首页>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——顺序结构
- ICTCLAS word Lucene 4.9 binding
- Let's talk about threadlocalinsecurerandom
- 1: Citation;
- leetcode刷题:二叉树18(最大二叉树)
- ffplay文档[通俗易懂]
- C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
- third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
- Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
- 四万字长文说operator new & operator delete
猜你喜欢
城链科技数字化创新战略峰会圆满召开
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
How to select the Block Editor? Impression notes verse, notation, flowus
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
leetcode刷题:二叉树16(路径总和)
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Let's talk about threadlocalinsecurerandom
The city chain technology Digital Innovation Strategy Summit was successfully held
C application interface development foundation - form control (5) - grouping control
随机推荐
leetcode刷题:二叉树13(相同的树)
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
How to select the Block Editor? Impression notes verse, notation, flowus
【c语言】归并排序
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
Jvmrandom cannot set seeds | problem tracing | source code tracing
DP: tree DP
银河证券在网上开户安全吗?
Multi branch structure
Zero cloud new UI design
Is the education of caiqiantang reliable and safe?
Leetcode brush questions: binary tree 11 (balanced binary tree)
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
leetcode刷题:二叉树10(完全二叉树的节点个数)
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
leetcode刷题:二叉树14(左叶子之和)
国信证券在网上开户安全吗?
How to safely and quickly migrate from CentOS to openeuler
DP:树DP
炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗