当前位置:网站首页>257. All paths of binary tree
257. All paths of binary tree
2022-07-03 12:13:00 【zwanying】
subject
Give you the root node of a binary tree root , Press In any order , Return all paths from the root node to the leaf node .
Leaf node A node without children .
Example 1:
Input :root = [1,2,3,null,5]
Output :[“1->2->5”,“1->3”]
Example 2:
Input :root = [1]
Output :[“1”]
Tips :
The number of nodes in the tree is in the range [1, 100] Inside
-100 <= Node.val <= 100
Their thinking
Find the way , It seems that there is something wrong with traversal no matter how :
After finding the first leaf node , How to get back , Continue to traverse the next leaf Node ? This requires backtracking .
Record the traversed path with an array , Find the leaf node , Go back to the previous node .
Use recursion to solve , Where to recurse , Where to go back .
Code implementation
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<Integer> list = new ArrayList<>();
List<String> result = new ArrayList<>();
if(root == null) return result;
digui(list,result,root);
return result;
}
void digui(List<Integer> list,List<String> result,TreeNode r){
list.add(r.val);
if( r.left ==null && r.right == null){
String ss = "";
for(int i=0;i<list.size();i++){
ss = ss + Integer.toString(list.get(i));
if(i!=list.size()-1){
ss = ss + "->";
}
}
result.add(ss);
return;
}
if(r.left != null){
digui(list,result,r.left);
list.remove(list.size()-1);
}
if(r.right != null){
digui(list,result,r.right);
list.remove(list.size()-1);
}
}
}
边栏推荐
- Flutter: about monitoring on flutter applications
- (construction notes) ADT and OOP
- Download address and installation tutorial of vs2015
- Visual studio 2022 downloading and configuring opencv4.5.5
- Shutter: about inheritedwidget
- Shell: basic learning
- laravel 时区问题timezone
- [learning notes] DP status and transfer
- Vulnhub narak
- LeetCode 0556.下一个更大元素 III - 4步讲完
猜你喜欢

Momentum of vulnhub

网络通讯之Socket-Tcp(一)

Php Export word method (One MHT)

Itext7 uses iexternalsignature container for signature and signature verification

AOSP ~ NTP (Network Time Protocol)

Unity3d learning notes 5 - create sub mesh

Shutter: overview of shutter architecture (excerpt)

ES6 standard

win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法

vulnhub之Ripper
随机推荐
STL tutorial 10 container commonalities and usage scenarios
836. Merge sets (day 63) and search sets
shardingSphere分库分表<3>
vulnhub之Ripper
vulnhub之pyexp
Notes on 32-96 questions of sword finger offer
Groovy test class and JUnit test
Is BigDecimal safe to calculate the amount? Look at these five pits~~
Flutter 退出登录二次确认怎么做才更优雅?
Differences between MySQL Union and union all
Kubernetes three dozen probes and probe mode
PHP导出word方法(一phpword)
Dart: About zone
STL Tutorial 9 deep copy and shallow copy of container elements
[learning notes] DP status and transfer
Vulnhub geminiinc
vulnhub之cereal
XML (DTD, XML parsing, XML modeling)
[official MySQL document] deadlock
Unicode encoding table download