Traversal of binary trees, recursion and non-recursion

Source: Internet
Author: User

Import Java.util.arraydeque;import java.util.stack;class TreeNode {public int val;public TreeNode Left;public TreeNode Right;public TreeNode (int val) {this.val = val;}} public class binarytree{//first-order traversal recursive public static void preorder (TreeNode t) {if (t = = null) {return;} System.out.print (t.val+ "");p reorder (t.left);p reorder (t.right);} Middle order traversal recursive public static void Inorder (TreeNode t) {if (t = = null) {return;} Inorder (T.left); System.out.print (t.val+ ""); Inorder (t.right);} Post-traversal recursive public static void Postorder (TreeNode t) {if (t = = null) {return;} Postorder (T.left);p Ostorder (t.right); System.out.print (t.val+ "");} First-order traversal non-recursive public static void PreOrder2 (TreeNode t) {stack<treenode> s = new stack<> (); while (T!=null | |! S.isempty ()) {while (t! = null)//traverse the root node and the left subtree {System.out.print (t.val+ ""); S.push (t); t = T.left;} if (!s.isempty ())//Traverse right child {t = S.pop (); t = T.right;}}} Middle order traversal non-recursive public static void InOrder2 (TreeNode t) {stack<treenode> s = new stack<> (); while (T!=null | |! S.isempty ()) {while (t! = null) {S.push (t); t = T.left;} if (!s.isempty ()) {t = S.pop (); System.out.println (t.val); t = T.right;}}} Post-traversal non-recursive public static void PostOrder2 (TreeNode t) {stack<treenode> s = new stack<> (); stack<treenode> result = new stack<> ();//Store counter-sequential traversal result while (t! = null | |!s.isempty ()) {while (t! = null) {S.push (t ); Result.push (t); t = T.right;} if (!s.isempty ()) {t = S.pop (); t = T.left;}} while (!result.isempty ()) {System.out.print (Result.pop (). val+ "");}} Sequence traversal public static void Leveltraverse (TreeNode t) {arraydeque<treenode> queue = new arraydeque<> (); if (t = = NULL) {return;} Queue.offer (t); while (!queue.isempty ()) {t = Queue.poll (); System.out.print (T.val + ""); if (t.left! = null) {Queue.offer (t.left);} if (t.right! = null) {Queue.offer (t.right);}}} Spiral traverse public static void Spiraltraverse (TreeNode t) {stack<treenode> s1 = new stack<> (); stack<treenode> s2 = new stack<> (); if (t = = null) {return;} S1.push (t); while (!s1.isempty () | |!s2.isempty ()) {while (!s1.isempty ()) {t = S1.pop (); System.Out.print (t.val+ ""); if (t.right!=null) {S2.push (t.right);} if (t.left!=null) {S2.push (t.left);}} while (!s2.isempty ()) {t = S2.pop (); System.out.print (t.val+ ""); if (t.left!=null) {S1.push (t.left);} if (t.right!=null) {S1.push (t.right);}}}}

Traversal of binary trees, recursion and non-recursion

Related Article

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.