9-degree OJ1521: Binary Tree image, offeroj1521
Question link:
Http://ac.jobdu.com/problem.php? Pid = 1, 1521
Question 1521: Binary Tree Image
Time Limit: 1 second memory limit: 128 MB Special Judgment: No submitted: 2075 resolution: 564
Description:
Input a binary tree and output its image.
Input:
The input may contain multiple test examples. The input ends with EOF.
For each test case, the first input behavior is an integer n (0 <=n <= 1000, and n represents the number of Binary Tree nodes to be input (nodes start from 1 ). The next row contains n numbers, representing the value of the element of the I-th Binary Tree node. Next there are n rows, each row has a letter Ci.
Ci = 'D' indicates that the I node has two children, followed by the left and right children numbers.
Ci = 'l' indicates that the I node has a left child followed by the ID of the left child.
Ci = 'R' indicates that the I node has a right child followed by the number of the right child.
Ci = 'Z' indicates that no child exists on the I node.
Output:
Corresponding to each test case,
Output the element values of the child node in the forward order.
If it is NULL, the output is NULL.
Sample input:
7
8 6 10 5 7 9 11
D 2 3
D 4 5
D 6 7
Z
Z
Z
Z
Sample output:
8 10 11 9 6 7 5
Train of Thought Analysis:
Binary Tree Image Process:
Each node of the binary tree is traversed in the previous order. If the node has a subnode, the left and right child nodes are swapped, and then recursive down until the first leaf node ends.
Each vertex of a binary tree is accessed once, so the time complexity is O (n ).
Space complexity O (1 ).
This algorithm understands that the code is easy to write, and it is difficult to torture people in the test cases and input and output formats. For interview preparation, it seems a waste of time to stick to these AC too much.
Note:
- Because the input contains numbers and characters, you must consider the problem of the input buffer queue. If a line contains characters, a line break will be left at the end. When the next line is read, an error character will be read. Therefore, before entering the data in the next row, refresh the buffer using while (ch = getchar ())! = '\ N'); while-getchar () to eat' \ n' entered by scanf '.
- In the output format, each output node must have a space and no space after the last element is output. Instead, a line break is output.
- In the test case, the root node is the root node and the question is not specified. Therefore, the root node is determined based on the inbound level, and the root node inbound level is 0. (I am stuck here, WA. Subconsciously think that the first element is the root node .......)
How can we determine the root node and construct a binary tree based on the inbound level:
For more detailed handling methods, see the code comment !!
Code:
/*********************************** [Sword refers to the Offer interview question] jiudu OJ1521: binary Tree image Author: Muzhi, Date: 2015 Email: bzhou84@163.com **********************************/# include <stdio. h> # include <stdlib. h> # include <string> # include <math. h ># include <stack >#include <vector> # include <iostream> using namespace std; # define MAX 1005 // defines the Node typedef struct Node of the Binary Tree {int data; node * lchild; Node * rchild; bool isRootNode; // indicates whether the Node is the root Node} BTNode; BTNode biTNode [MAX]; // an array of all nodes of a binary tree // swap the left and right children of a non-leaf node of the binary tree to obtain the image void MirrorTree (BTNode * pNode) of the binary tree) {if (NULL = pNode | (pNode-> lchild = NULL & pNode-> rchild = NULL) return; BTNode * temp; temp = pNode-> lchild; pNode-> lchild = pNode-> rchild; pNode-> rchild = temp; MirrorTree (pNode-> lchild ); mirrorTree (pNode-> rchild);} // construct a binary tree BTNode * createBTree (int n) with n nodes {BTNode * rootNode = NULL; // Root Node If (n = 0) // return rootNode; char Ci; // character, Ci indicates the number of child node data fields, the serial number of the left child in the biTNode [] of the binary tree node and the serial number int I, data, lChildNumber, rChildNumber of the right child in the biTNode []; // first constructed, consider each node as a binary tree (I = 1; I <= n; I ++) {scanf ("% d ", & data); biTNode [I]. data = data; biTNode [I]. lchild = NULL; biTNode [I]. rchild = NULL; biTNode [I]. isRootNode = true;} // construct the left and right child nodes of each node based on the link between each node, and merge each node into the binary tree to be constructed for (I = 1; I <= n; I ++) {char c; while (c = getchar ())! = '\ N'); // eat the' \ n' scanf ("% c", & Ci) of the line entered by scanf; switch (Ci) {case 'D': scanf ("% d", & lChildNumber, & rChildNumber); biTNode [I]. lchild = & biTNode [lChildNumber]; // locate the left and right child biTNode [I]. rchild = & biTNode [rChildNumber]; biTNode [lChildNumber]. isRootNode = false; // biTNode [lChildNumber] is not the root node of a binary tree. biTNode [rChildNumber]. isRootNode = false; break; case 'l': scanf ("% d", & lChildNumber); biTNode [I]. lchild = & biTNod E [lChildNumber]; biTNode [I]. rchild = NULL; biTNode [lChildNumber]. isRootNode = false; break; case 'r': scanf ("% d", & rChildNumber); biTNode [I]. lchild = NULL; biTNode [I]. rchild = & biTNode [rChildNumber]; biTNode [rChildNumber]. isRootNode = false; break; case 'Z': biTNode [I]. lchild = NULL; biTNode [I]. rchild = NULL; break; default: break;} // If a node is not a child node of any node, the node is the root node for (I = 1; I <= n; I ++) {if (biTNo De [I]. isRootNode = true) {rootNode = & biTNode [I]; // determine the root node break;} return rootNode ;} // forward traversal of the output binary tree void PreOrder (BTNode * pNode) {if (pNode = NULL) return; printf ("% d", pNode-> data ); // The output format is space PreOrder (pNode-> lchild); PreOrder (pNode-> rchild);} int main () {int n; BTNode * root; while (scanf ("% d", & n )! = EOF) {root = createBTree (n); if (root = NULL) {printf ("NULL \ n");} else {MirrorTree (root ); printf ("% d", root-> data); // output format: PreOrder (root-> lchild); PreOrder (root-> rchild ); printf ("\ n"); // output format final} return 0 ;} /*************************************** * *********************** Problem: 1521 Language: C ++ Result: Accepted Time: 0 MS Memory: 1552 kb ************************************** **************************/
Summary
- Swap the left and right child nodes of the non-leaf nodes in the binary tree to obtain the binary tree image.
- The input contains numbers and characters.
- How to judge the root node based on the inbound degree
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.