Swap the left and right subtrees of the binary tree (flip of the binary tree) __ Two fork Tree

Source: Internet
Author: User

For the binary tree, we must be proficient in its various operations, today we want to implement the two-fork tree flip, that is, swap left and right subtree. The specific idea is not difficult, if a node is a leaf node, then do not operate; If a node has only a left child or a right child, it is exchanged, the original child is empty; If a node has both a left child and a right child, the child is exchanged. Detailed code uploaded to Https://github.com/chenyufeng1991/ReverseLeftRightChild.

The core code is as follows:

Swap left
and right subtree void Reverseleftrightchild (Bitnode **t)
{
    //If leaf node, recursive End If
    (*t = = NULL)
    {
        return ;
    }

    Swap ((*t)->lchild, (*t)->rchild); It is convenient to use the SWAP function directly, direct exchange pointer,
    Reverseleftrightchild (& ((*t)->lchild));
    Reverseleftrightchild (& (*t)->rchild);
}


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.