[Programmer interview questions selected 100 questions] 11. Search for binary tree images.

Source: Internet
Author: User

[Programmer interview questions selected 100 questions] 11. Search for binary tree images.
[Question]

Enter a binary search tree and convert it to its image. That is, in the converted Binary Search Tree, the nodes in the left subtree are greater than those in the right subtree. Use recursive and cyclic methods to convert tree images.

For example, enter:

8
/\
6 10
/\/\
5 7 9 11

Output:

8
/\
10 6
/\/\
11 9 7 5

[Analysis]



[Code]

/********************************** Date: * Author: SJF0115 * Subject: 11. find the image of the binary search tree * Source: * category: select **********************************/ # include <iostream> using namespace std; struct TreeNode {int val; TreeNode * left; TreeNode * right; TreeNode (int x): val (x), left (NULL), right (NULL ){}}; // obtain the image void BinaryTreeMirror (TreeNode * & root) {if (root = NULL) {return;} TreeNode * p = root; // left and right subnodes exchange TreeNode * tmp; tmp = p-> left; p-> left = p-> right; p-> right = tmp; // left subtree if (p-> left) {BinaryTreeMirror (p-> left);} // if // right subtree if (p-> right) {BinaryTreeMirror (p-> right);} // if} // traverse void InOrder (TreeNode * root) {if (root = NULL) {return ;} if (root-> left) {InOrder (root-> left) ;}cout <root-> val <endl; if (root-> right) {InOrder (root-> right) ;}// insert void TreeInsert (TreeNode * & root, int val) to the Binary Search Tree) {// create a new node TreeNode * node = new TreeNode (val); if (root = NULL) {root = node;} else {TreeNode * pre = NULL; treeNode * cur = root; // find the insert position while (cur) {// parent node pre = cur; // drop if (val <cur-> val) along the left subtree) {cur = cur-> left;} // else is dropped along the right subtree. {cur = cur-> right ;}} // while // Insert the left subnode if (val <pre-> val) {pre-> left = node ;} // insert else {pre-> right = node;} // if} int main () {int array [] = {8, 6, 10, 5, 7, 9, 11}; // create a binary search tree TreeNode * root = NULL; for (int I = 0; I <7; I ++) {TreeInsert (root, array [I]);} // binary tree image BinaryTreeMirror (root); // traverse InOrder (root) in the middle order; return 0 ;}

Code 2]

// Obtain the image void BinaryTreeMirror (TreeNode * & root) {if (root = NULL) {return;} stack <TreeNode *> stack; treeNode * p = root; // first traverse while (p! = NULL |! Stack. empty () {if (p! = NULL) {// left and right subnodes exchange TreeNode * tmp; tmp = p-> left; p-> left = p-> right; p-> right = tmp; // stack. push (p); // access the left subtree p = p-> left;} // if else {// return stack p = stack. top (); stack. pop (); // access the right subtree p = p-> right ;}}// while}

[Resolution 2]

Because the essence of recursion is that the compiler generates a function call stack, the simplest way to use a loop to complete the same task is to use an auxiliary stack to simulate recursion. First, we put the header node of the tree into the stack. In a loop, as long as the stack is not empty, the top node of the stack pops up and switches its left and right subtree. If it has a left subtree, press its left subtree into the stack; if it has a right subtree, press its right subtree into the stack. In this way, the left and right subtree of its son node can be exchanged in the next loop.

Code 3]

// Obtain the image void BinaryTreeMirror (TreeNode * & root) {if (root = NULL) {return;} stack <TreeNode *> stack; stack. push (root); TreeNode * p = NULL; while (! Stack. empty () {p = stack. top (); stack. pop (); // left and right subnodes exchange TreeNode * tmp; tmp = p-> left; p-> left = p-> right; p-> right = tmp; // The left subnode is not empty and is pushed into the stack if (p-> left) {stack. push (p-> left);} // if (p-> right) {stack. push (p-> right); }}// while}





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.