"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)"