Topic:
The middle sequence traversal of a two-fork tree
Give a binary tree, return the sequence traversal
Sample Example
Give a two-fork tree {1,#,2,3}
,
1 2 / 3
Return [1,3,2]
.
challenges
Can you use non-recursive algorithms to do this?
Solving:
Direct source of the program
Java Program:
/*** Definition of TreeNode: * public class TreeNode {* public int val; * Public TreeNode left, right; * p Ublic TreeNode (int val) {* This.val = val; * This.left = This.right = null; *} *}*/ Public classSolution {/** * @paramroot:the root of binary tree. * @return: Inorder in ArrayList which contains node values. */ PublicArraylist<integer>inordertraversal (TreeNode root) {//Write your code hereArraylist<treenode> p =NewArraylist<treenode>(); ArrayList<Integer> res =NewArraylist<integer>(); while(Root! =NULL|| P.size ()! = 0){ while(Root! =NULL) {p.add (root); Root=Root.left; } Root= P.get (P.size ()-1); P.remove (P.size ()-1); Res.add (Root.val); Root=Root.right; } returnRes; } }
View Code
Total time: 1238 Ms
Python program:
"""Definition of Treenode:class treenode:def __init__ (Self, val): Self.val = Val Self.left, self.right = None, none"""classSolution:"""@param root:the root of binary tree. @return: Inorder in ArrayList which contains node values. """ definordertraversal (self, root):#Write your code herep =[Root] res=[0] whileRoot is notNoneorLen (p)! = 1: whileRoot is notNone:p.append (root) root=Root.left Root= P[len (p)-1] delP[len (p)-1] Res.append (root.val) root=root.right N=Len (res)returnRES[1:N]
View Code
Total time: 263 ms
Non-recursive procedures, understanding, but also need people ugly to read more
According to the above inspiration, the recursive procedure is as follows:
Java Program:
/*** Definition of TreeNode: * public class TreeNode {* public int val; * Public TreeNode left, right; * PU Blic TreeNode (int val) {* This.val = val; * This.left = This.right = null; *} *}*/ Public classSolution {/** * @paramroot:the root of binary tree. * @return: Inorder in ArrayList which contains node values. */ PublicArraylist<integer>inordertraversal (TreeNode root) {//Write your code hereArrayList<Integer> res =NewArraylist<integer>(); Res=Inordertrun (res,root); returnRes; } PublicArraylist<integer> Inordertrun (arraylist<integer>Res,treenode Root) { if(Root = =NULL) returnRes; if(root!=NULL){ if(root.left!=NULL) {res=Inordertrun (Res,root.left); } res.add (Root.val); if(root.right!=NULL) {res=Inordertrun (res,root.right); } } returnRes; } }
View Code
Total time: 1714 Ms
Python program:
"""Definition of Treenode:class treenode:def __init__ (Self, val): Self.val = Val Self.left, self.right = None, none"""classSolution:"""@param root:the root of binary tree. @return: Inorder in ArrayList which contains node values. """ definordertraversal (self, root):#Write your code hereres =[] Res=Self.inordertrun (res,root)returnResdefInordertrun (self,res,root):ifroot==None:returnResifroot.left!=None:res=Self.inordertrun (res,root.left) res.append (root.val)ifroot.right!=None:res=Self.inordertrun (res,root.right)returnRes
View Code
Total time: 213 ms
Lintcode Easy title: Binary Tree inorder traversal two-in-sequence traversal