Converts the leaf node of a binary tree to a single-chain table, and returns the address (chain header) of the leftmost leaf node)

Source: Internet
Author: User

The linked list storage structure of a known binary tree is defined as follows:

Typedef struct bintnode
{
Int data;
Bintnode * lchild;
Bintnode * rchild;

Bintnode (INT elemet = 0, bintnode * Left = NULL, bintnode * Right = NULL)
: Data (elemet), lchild (left), rchild (right ){}
} Bintnode;

Write a recursive algorithm that uses the left-right link of the leaf node to link all leaf nodes from left to right into a single-chain table, the algorithm returns the address (chain header) of the leftmost leaf node ).

Algorithm Analysis: You can traverse the binary tree in the First Order, "Left and Right "!

# Include "stdafx. H "# include <iostream> using namespace STD; typedef struct bintnode {int data; bintnode * lchild; bintnode * rchild; bintnode (INT elemet = 0, bintnode * Left = NULL, bintnode * Right = NULL): Data (elemet), lchild (left), rchild (right) {}} bintnode; // recursively create a binary tree bintnode * createbintree (INT arr [], int begin, int end) {bintnode * root = NULL; If (begin> = END) return root; root = new bintnode (ARR [begin]); root -> Lchild = createbintree (ARR, begin * 2 + 1, end); root-> rchild = createbintree (ARR, begin * 2 + 2, end); Return root ;} // function: recursive. Use the left-right pointer field rchild in the leaf node to link all leaf nodes from left to right into a single-chain table, return the leftmost leaf node address bintnode * head = NULL; bintnode * temp = NULL; void changletosinglelist (bintnode * root) {If (null = root) // The tree is empty return; // The leaf node if (root-> lchild = NULL & root-> rchild = NULL) {If (null = temp) {temp = head = root;} else {temp-> r Child = root; temp = root; }}changletosinglelist (root-> lchild); // left subtree changletosinglelist (root-> rchild); // right subtree} int main () {const int n = 10; int arr [N]; for (INT I = 0; I <n; I ++) Arr [I] = I + 1; bintnode * root = createbintree (ARR, 0, n); changletosinglelist (Root); While (Head! = NULL) {cout 

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.