"Leetcode-Interview algorithm classic-java implementation" "094-binary Tree inorder Traversal (binary trees in sequence traversal)"

Source: Internet
Author: User

"094-binary Tree inorder Traversal (binary tree sequence traversal)""leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"Original Question

Given a binary tree, return the inorder traversal of its nodes ' values.

Main Topic

A binary tree is traversed in sequence.

Thinking of solving problems

Solution One: Recursive implementation, solution two: iterative implementation.

Code Implementation

Binary tree Node class

publicclass TreeNode {    int val;    TreeNode left;    TreeNode right;    TreeNode(int x) { val = x; }}

Algorithm implementation class, solution one

ImportJava.util.LinkedList;ImportJava.util.List; Public  class solution {    Privatelist<integer> result; PublicList<integer>Inordertraversal(TreeNode Root) {result =NewLinkedlist<> (); Inorder (root);returnResult }Private void inorder(TreeNode Root) {if(Root! =NULL) {inorder (root.left);            Result.add (Root.val);        Inorder (Root.right); }    }}

Algorithm implementation class, solution one

ImportJava.Util.LinkedList;ImportJava.Util.List; PublicClass Solution { Public List<Integer>Inordertraversal (TreeNode root) {List<Integer>Result= NewLinkedList<>(); LinkedList<TreeNode> Stack = NewLinkedList<>(); TreeNode node=Root while(node!= NULL || !Stack.IsEmpty ()) { while(node!= NULL) {Stack.AddLast (node); Node=Node.Left }if(!Stack.IsEmpty ()) {node= Stack.Removelast (); Result.Add (node.Val); Node=Node.Right }} System.Out.println (result);returnResult }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Solution One

Solution Two

Special InstructionsWelcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47310553"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java implementation" "094-binary Tree inorder Traversal (binary trees 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.