Non-recursive binary tree traversal
Binary tree traversal is one of the most basic algorithms of a tree and is the basis for other operations on a binary tree.
The so-called Traversal (Traversal) refers to the sequential access to each node in the tree along a certain search route.
The operations performed by the Access Node depend on specific application problems.
① PreorderTraversal (PreorderTraversal ))
-- The operation to access the root node occurs before traversing the left and right subtree.
② InorderTraversal)
-- The operation to access the root node occurs in traversing the Left and Right sub-trees ).
③ Post-sequential traversal (PostorderTraversal)
-- The operation to access the root node occurs after traversing the Left and Right sub-trees.
④ LevelTraversal)
-- Access layer-by-layer access starting from the root node
Import java. util. arrayList; import java. util. arrays; import java. util. using list; import java. util. stack; // class TreeNode {int val; TreeNode left; TreeNode right; TreeNode (int x) {val = x ;}} public class TestTraversalTreeNode {/*** line traversal ** @ param root * @ return */public static ArrayList
PreorderTraversal (TreeNode root) {ArrayList
Result = new ArrayList
(); If (root = null) return result; Stack
Stack = new Stack
(); Stack. push (root); while (! Stack. isEmpty () {TreeNode t = stack. pop (); result. add (t. val); // first check if (t. right! = Null) {stack. push (t. right);} if (t. left! = Null) {stack. push (t. left) ;}} return result;}/*** sequential traversal ** @ param root * @ return */public static ArrayList
InorderTraversal (TreeNode root) {ArrayList
Result = new ArrayList
(); If (root = null) return result; Stack
Stack = new Stack
(); TreeNode p = root; // if there is a left node, pushwhile (! Stack. isEmpty () | p! = Null) {if (p! = Null) {stack. push (p); p = p. left;} else {TreeNode n = stack. pop (); result. add (n. val); p = n. right;} return result;}/*** post-order traversal ** @ param root * @ return */public static ArrayList
PostorderTraversal (TreeNode root) {ArrayList
Result = new ArrayList
(); If (root = null) {return result;} Stack
Stack = new Stack
(); Stack. push (root); TreeNode prev = null; // record the previous node of the current node while (! Stack. empty () {TreeNode curr = stack. peek (); // check whether the current node is a leaf node. if yes, access if (prev = null | prev. left = curr | prev. right = curr) {if (curr. left! = Null) {stack. push (curr. left);} else if (curr. right! = Null) {stack. push (curr. right);} else {// the current node is a leaf node stack. pop (); result. add (curr. val);} // check whether prev is the current node's left node} else if (curr. left = prev) {if (curr. right! = Null) {stack. push (curr. right);} else {stack. pop (); result. add (curr. val);} // check whether prev is the right node of the current node} else if (curr. right = prev) {stack. pop (); result. add (curr. val);} prev = curr;} return result;}/*** layered traversal ** @ param root * @ return */public static ArrayList
LevelTraversal (TreeNode root) {ArrayList
Result = new ArrayList
(); Parameter list
Current = new consumer list
(); If (root! = Null) {current. add (root); result. add (root. val);} while (current. size ()> 0) {values list
Parents = current; current = new rule list
(); For (TreeNode parent: parents) {if (parent. left! = Null) {current. add (parent. left); result. add (parent. left. val);} if (parent. right! = Null) {current. add (parent. right); result. add (parent. right. val) ;}} return result ;} /*** traverse the k row of the Binary Tree ** @ param root binary root * @ param k * @ return the traversal of the k row */public static String findLevelList2 (TreeNode root, int k) {ArrayList
> Result = new ArrayList
> (); Consumer list
Current = new consumer list
(); If (root! = Null) {current. add (root);} int count = 0; while (current. size ()> 0) {result. add (current); if (count = k) {return listToString (current);} count ++; returns list
Parents = current; current = new rule list
(); For (TreeNode parent: parents) {if (parent. left! = Null) {current. add (parent. left);} if (parent. right! = Null) {current. add (parent. right) ;}}return null;}/*** the node of the linked list is converted to a String for output * @ param list * @ return */public static String listToString (sorted list
List) {int [] arr = new int [list. size ()]; int I = 0; for (TreeNode node: list) {arr [I] = node. val; I ++;} return Arrays. toString (arr);} public static void main (String [] args) {TreeNode root = new TreeNode (1); root. left = new TreeNode (2); root. right = new TreeNode (3); root. left. left = new TreeNode (4); root. left. right = new TreeNode (5); System. out. println ("Preface:" + preorderTraversal (root ). toString (); System. out. println ("Middle Order:" + inorderTraversal (root ). toString (); System. out. println ("Suffix:" + postorderTraversal (root ). toString (); System. out. println ("sequence:" + levelTraversal (root ). toString (); System. out. println ("K sequence:" + findLevelList2 (root, 2 ));}}