Sword Point offer surface test (Java version): Two image of the fork Tree

Source: Internet
Author: User

Title: Please complete a function, enter a binary tree, the function output its image

The structure of a binary tree is defined as:

Package Utils;public class Binarytreenode {public int value;public Binarytreenode Leftnode;public binarytreenode Rightnode;public Binarytreenode () {}public binarytreenode (int value) {this.value = value; this.leftnode = null; This.rightnode = null;}}
The image of a tree is a relatively new concept, and we may not be able to find a way to mirror the tree in a single swoop. To be able to create an intuitive impression, we can draw a binary tree ourselves, and then draw its mirror image based on the experience of the mirror.
: The two fork tree on the right is the mirror of the tree on the left.


Carefully analyze the characteristics of the two trees to see if you can summarize the steps for mirroring. The root nodes of the two trees are the same, but their left and right two sub-nodes exchange positions. So we might as well first swap the two nodes of the root node in the tree and get the second tree below


After swapping the two subnodes of the root node, we notice that the child nodes of the nodes with the value 10,6 remain unchanged, so we also need to swap the left and right child nodes of the two nodes. The results after the exchange were the third lesson tree and the fourth tree. After these two exchanges, we have traversed all the non-leaf nodes. The tree after the swap is just the mirror of the original tree.

Summing up the process above, we draw the process of finding a tree's image: We iterate through each node of the tree, and if the traversed node has a child node, it swaps its two sub-nodes, and after swapping all the non-leaf nodes of the left and right subnodes, we get the image.

Implemented in Java code:

/** * Title: Please complete a function, enter a binary tree, the function output its image. */package Swordforoffer;import Utils. binarytreenode;/** * @author Jinshuangqi * * August 1, 2015 */public class E19mirrorofbinarytree {public void mirrorrecursively (Binarytreenode node) {if (node = = null) return;if (Node.leftnode = = NULL && Node.rightnode = = null) return; Binarytreenode temp = Node.leftnode;node.leftnode = Node.rightnode;node.rightnode = Temp;if (Node.leftNode! = null) Mirrorrecursively (Node.leftnode); if (node.rightnode! = null) mirrorrecursively (Node.rightnode);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sword Point offer surface test (Java version): Two image of the fork Tree

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.