Binary Tree Image

Source: Internet
Author: User

// Binary Tree Structure class BinaryTreeNode {String data; BinaryTreeNode left; BinaryTreeNode right;}/*** enter a binary tree, output its image */public class BitreeMirrorImg {role = new role (System. in); // create a binary tree public BinaryTreeNode createTree (BinaryTreeNode root) {String data; data = metadata. next (); if (data. equals ("#") {return null;} root = new BinaryTreeNode (); root. data = data; root. left = createTree (root. left); root. right = cr EateTree (root. right); return root;} // obtain the binary tree image public void mirror (BinaryTreeNode root) {if (root = null) {return;} if (root. left = null) & (root. right = null) {return;} BinaryTreeNode temp = root. left; root. left = root. right; root. right = temp; mirror (root. left); mirror (root. right);} // layered traversal of the Binary Tree public void levelTraverse (BinaryTreeNode root) {java. util. repeated list <BinaryTreeNode> list = new java. util. revoke list <BinaryTreeNode> (); list. add (root); while (list. size ()! = 0) {BinaryTreeNode node = list. removeFirst (); System. out. print (node. data + ""); if (node. left! = Null) {list. add (node. left);} if (node. right! = Null) {list. add (node. right) ;}} public static void main (String [] args) {BitreeMirrorImg bmi = new BitreeMirrorImg (); BinaryTreeNode root = null; root = bmi. createTree (root); bmi. mirror (root); bmi. levelTraverse (root );}}

 

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.