Reprint Please specify source: http://blog.csdn.net/crazy1235/article/details/51474128
Subject
Source: https://leetcode.com/problems/binary-tree-paths/
returnall root-to-leaf paths.For example, given the following binary tree: 1/ \2 3\ 5All root-to-leaf paths are:["1->2->5""1->3"]
Explain
The topic is to find all the paths of a binary tree (from the root node to each leaf node).
Returns a list collection of all paths.
Solutionsolution 1
The easiest way to think of is recursion.
Depth-first traversal of-DFS.
publicList<String>=new ArrayList<String>();
/** * recursive way * * <br/> * * Run time:4ms * * @param root * @return */ public list<string> b Inarytreepaths (TreeNode root) {if (root = nul L ) {return resultlist; } list<string> Singleresult = new arraylist<> (); Gettreepath (Root, Singleresult); return resultlist; }
/** * DFS * * @param resultlist * @param node * @param Singleresult */ Private void Gettreepath(TreeNode node, list<string> singleresult) {Singleresult.add (Node.val +"");if(Node.left = =NULL&& Node.right = =NULL) {Resultlist.add (GetPath (Singleresult)); }if(Node.left! =NULL) {Gettreepath (Node.left,NewArraylist<> (Singleresult)); }if(Node.right! =NULL) {Gettreepath (Node.right,NewArraylist<> (Singleresult)); } }
Well, that's the easiest way to think about it.
I also thought of the stack to solve, but did not write out, embarrassed ~ ~ ~
Solution 2
Method two, similar to method one.
Reference: Https://leetcode.com/discuss/85025/sharing-my-recursive-java-solution
Publiclist<String> binaryTreePaths2 (TreeNode root) {if(Root = =NULL) {return resultlist; } findpaths (Root, Root.val +""); return resultlist; }Privatevoid Findpaths (TreeNode node,StringPath) {if(node. Left==NULL&& node. Right==NULL) {Resultlist.add (path); }if(node. Left!=NULL) {Findpaths (node. Left, Path +" ,"+ node. Left. val); }if(node. Right!=NULL) {Findpaths (node. Right, Path +" ,"+ node. Right. val); } }
It is also solved by recursion.
However, a single path does not need to be stored through a list collection and is constructed directly from a string.
Solution 3
On the basis of method two, it is more efficient to change string to StringBuilder.
The method utilizes the setLength () method of the StringBuilder class.
So clever ~
PublicList<string>BINARYTREEPATHS3(TreeNode Root) {Findpath (Root,NewStringBuilder ());returnResultlist; }Private void Findpath(TreeNode node, StringBuilder SB) {if(node = =NULL) {return; }intLen = Sb.length (); Sb.append (Node.val);if(Node.left = =NULL&& Node.right = =NULL) {Resultlist.add (sb.tostring ()); }Else{Sb.append (" ,"); Findpath (Node.left, SB); Findpath (Node.right, SB); } sb.setlength (len);//intercept!!! }
The test run time on the Leetcode platform is 2ms.
Solution 4
To do it through the stack.
hahaha.
Stack
Reference: Https://leetcode.com/discuss/83013/my-java-non-recursion-solution-using-stack-and-wrapper
Public list<string> binaryTreePaths4 (TreeNode root) {if (root = null) {return resultlist;} stack<wrapper> Stack = new Stack<> ();Stack. Add(New Wrapper (root, root. Val+""));Wrapper Wrapper = null;while (!stack. IsEmpty()) {wrapper = Stack. Pop();if (wrapper. Node. Left= = NULL && wrapper. Node. Right= = null) {Resultlist. Add(Wrapper. Path);} if (wrapper. Node. Left! = null) {stack. Add(New Wrapper (Wrapper. Node. Left, wrapper. Path+" ,"+ Wrapper. Node. Left. Val));} if (wrapper. Node. Right! = null) {stack. Add(New Wrapper (Wrapper. Node. Right, wrapper. Path+" ,"+ Wrapper. Node. Right. Val));}} return Resultlist;}
privatestaticclass Wrapper { TreeNode node; String path; publicWrapper() { } publicWrapper(TreeNode node) { this.node = node; } publicWrapper(TreeNode node, String path) { this.node = node; this.path = path; } }
A class is required to wrap, storing the current node, and accessing the path to the node.
If you look at the code is not understood, the direct breakpoint tracking a bit understand.
The method run time is 5ms
Solution 5
Since DFS can be implemented, it is also possible to estimate BFS.
Well. Is possible ~ ~
Reference: Https://leetcode.com/discuss/67749/bfs-with-two-queue-java-solution
/** * BFS * with the Queue * @param root * @return * * PublicList<string>BINARYTREEPATHS5(TreeNode Root) {if(Root = =NULL) {returnResultlist; } queue<treenode> Nodequeue =NewLinkedlist<treenode> (); Queue<string> Pathqueue =NewLinkedlist<> (); Nodequeue.offer (root); Pathqueue.offer (Root.val +""); while(!nodequeue.isempty ()) {TreeNode Currnode = Nodequeue.poll (); String item = Pathqueue.poll ();if(Currnode.left = =NULL&& Currnode.right = =NULL) {Resultlist.add (item); }if(Currnode.left! =NULL) {Nodequeue.offer (currnode.left); Pathqueue.offer (item +" ,"+ CurrNode.left.val); }if(Currnode.right! =NULL) {Nodequeue.offer (currnode.right); Pathqueue.offer (item +" ,"+ CurrNode.right.val); } }returnResultlist; }
Through two queues:
A storage node.
A store accesses the path to this node
The judging condition of the end of a single path is still the current node is the leaf node.
The method run time is 4ms.
Solution 6
The idea of the method is still a recursive operation.
However, you do not need to create an additional function to recursively invoke.
Direct recursive invocation of the provided function.
Reference: Https://leetcode.com/discuss/55451/clean-solution-accepted-without-helper-recursive-function
Publiclist<String> binaryTreePaths6 (TreeNode root) {list<String> pathslist =Newarraylist<String> ();if(Root = =NULL) {return pathslist; }if(Root. Left==NULL&& Root. Right==NULL) {Pathslist.add (Root.val +""); return pathslist; } for(StringPATH:BINARYTREEPATHS6 (Root. Left) {Pathslist.add (Root.val +" ,"+ path); } for(StringPATH:BINARYTREEPATHS6 (Root. Right) {Pathslist.add (Root.val +" ,"+ path); } return pathslist; }
Yes.
The method run time is 3ms.
source : GitHub, please.
https://github.com/crazy1235/BaseJavaProject/blob/master/BaseJavaProject/src/com/jacksen/java/leetcode/ Binarytreepaths.java
bingo~~
"Leetcode" 257. Binary Tree Paths Problem Solving report