[Sword refers to Offer learning] [interview 19: Binary Tree image], sword refers to offer

Source: Internet
Author: User

[Sword refers to Offer learning] [interview 19: Binary Tree image], sword refers to offer
Question: complete a function and enter a binary tree. The function outputs its image. Binary Tree node definition:

/*** Tree node of the binary tree */public static class BinaryTreeNode {int value; BinaryTreeNode left; BinaryTreeNode right ;}
Solution:

We previously traverse every node of the tree in order. If the node to be traversed has a child node, we will exchange two of its child nodes. After switching all the left and right subnodes of non-leaf nodes, the tree image is obtained.

Code implementation:
Public class Test19 {/*** Tree node of the binary tree */public static class BinaryTreeNode {int value; BinaryTreeNode left; BinaryTreeNode right;}/*** complete a function and enter... Binary Tree. This function outputs its image ** @ param node root node */public static void mirror (BinaryTreeNode node) {// if the current node is not empty, operate if (node! = Null) {// The following is the BinaryTreeNode tmp = node of the left and right subtree of the exchange node. left; node. left = node. right; node. right = tmp; // process mirror (node. left); mirror (node. right) ;}} public static void printTree (BinaryTreeNode node) {if (node! = Null) {printTree (node. left); System. out. print (node. value + ""); printTree (node. right) ;}} public static void main (String [] args) {// 8 ////// 6 10 /////\// 5 7 9 11 BinaryTreeNode root = new BinaryTreeNode (); root. value = 8; root. left = new BinaryTreeNode (); root. left. value = 6; root. left. left = new BinaryTreeNode (); root. left. left. value = 5; root. left. right = new BinaryTreeNode (); root. left. right. value = 7; root. right = new BinaryTreeNode (); root. right. value = 10; root. right. left = new BinaryTreeNode (); root. right. left. value = 9; root. right. right = new BinaryTreeNode (); root. right. right. value = 11; printTree (root); System. out. println (); mirror (root); printTree (root ); // 1 // 3 // 5 // 7 // 9 BinaryTreeNode root2 = new BinaryTreeNode (); root2.value = 1; root2.left = new BinaryTreeNode (); root2.left. value = 3; root2.left. left = new BinaryTreeNode (); root2.left. left. value = 5; root2.left. left. left = new BinaryTreeNode (); root2.left. left. left. value = 7; root2.left. left. left. left = new BinaryTreeNode (); root2.left. left. left. left. value = 9; System. out. println ("\ n"); printTree (root2); System. out. println (); mirror (root2); printTree (root2 ); // 0 // 2 // 4 // 6 // 8 BinaryTreeNode root3 = new BinaryTreeNode (); root3.value = 0; root3.right = new BinaryTreeNode (); root3.right. value = 2; root3.right. right = new BinaryTreeNode (); root3.right. right. value = 4; root3.right. right. right = new BinaryTreeNode (); root3.right. right. right. value = 6; root3.right. right. right. right = new BinaryTreeNode (); root3.right. right. right. right. value = 8; System. out. println ("\ n"); printTree (root3); System. out. println (); mirror (root3); printTree (root3 );}}
Running result:

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.