[Sword refers to Offer learning] [interview question 61: print a binary tree in the alphabetic order], sword refers to offer

Source: Internet
Author: User

[Sword refers to Offer learning] [interview question 61: print a binary tree in the alphabetic order], sword refers to offer
Question: Please implement a function to print a binary tree in the font order, that is, the first line is printed from left to right, and the second layer is printed from right to left, that is, the first row is printed from left to right, the second row is printed from right to left, and the third row is printed from left to right.Solutions

Two stacks are required to print a binary tree in the alphabetic order. When printing a line of knots, we save the child nodes of the next layer to the corresponding stack. If the print is an odd layer, save the left child node and the right child node to a stack. If the print is an even layer, save the right child node and then save the left child node to the Second stack.

Node Definition
private static class BinaryTreeNode {    private int val;    private BinaryTreeNode left;    private BinaryTreeNode right;    public BinaryTreeNode() {    }    public BinaryTreeNode(int val) {        this.val = val;    }    @Override    public String toString() {        return val + "";    }}
Code Implementation
Import java. util. using list; import java. util. list; public class Test61 {private static class BinaryTreeNode {private int val; private BinaryTreeNode left; private BinaryTreeNode right; public BinaryTreeNode () {} public BinaryTreeNode (int val) {this. val = val ;}@ Override public String toString () {return val + "" ;}} public static void print (BinaryTreeNode root) {if (root = null) {return ;} List <BinaryTreeNode> current = new Response List <> (); List <BinaryTreeNode> reverse = new Response List <> (); int flag = 0; BinaryTreeNode node; current. add (root); while (current. size ()> 0) {// get node = current from the last one. remove (current. size ()-1); System. out. printf ("%-3d", node. val); // The current value is printed from left to right, and then the stack is pushed from left to right. if (flag = 0) {if (node. left! = Null) {reverse. add (node. left);} if (node. right! = Null) {reverse. add (node. right) ;}}// the current data is printed from right to left, and the else {if (node. right! = Null) {reverse. add (node. right);} if (node. left! = Null) {reverse. add (node. left) ;}} if (current. size () = 0) {flag = 1-flag; List <BinaryTreeNode> tmp = current; current = reverse; reverse = tmp; System. out. println () ;}} public static void main (String [] args) {BinaryTreeNode n1 = new BinaryTreeNode (1); BinaryTreeNode n2 = new BinaryTreeNode (2 ); binaryTreeNode n3 = new BinaryTreeNode (3); BinaryTreeNode n4 = new BinaryTreeNode (4); BinaryTreeNode n5 = new BinaryTreeNode (5); BinaryTreeNode n6 = new BinaryTreeNode (6 ); binaryTreeNode n7 = new BinaryTreeNode (7); BinaryTreeNode n8 = new BinaryTreeNode (8); BinaryTreeNode region = new region (9); n1.left = n2; n1.right = n3; n2.left = n4; n2.right = n5; n3.left = n6; n3.right = n7; n4.left = n8; n4.right = bytes; print (n1 );}}
Running result

Description Please refer to the source for reprinting [http://blog.csdn.net/derrantcm/article/details/46857935]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.