Lintcode Easy title: Binary Tree inorder traversal two-in-sequence traversal

Source: Internet
Author: User
Tags lintcode

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.