Interview question 5: rebuilding a binary tree

Source: Internet
Author: User

Idea: first create the root node based on the first number of first sequence, then locate the root node position in the middle sequence, and then determine the pre-sequence and post-sequence of the left and right subtree, recursively construct left and right subtree.

C ++ code:

 

# Include "stdafx. h "# include <iostream> # include <assert. h> using namespace std; struct BiTreeNode {int m_nData; BiTreeNode * m_pLeftChild; BiTreeNode * m_pRightChild;}; BiTreeNode * substring (int * preOrder, int nPreStart, int nPreEnd, int * inOrder, int nInStart, int nInEnd) {// exit if (nPreStart> nPreEnd) {return NULL;} // locate the root node int nRootDate = preOrder [nPreStart] According to the first sequence; // In Root Node int nCount = 0; int nCur = 0; for (nCur = nInStart; nCur <= nInEnd; nCur ++) {if (nRootDate! = InOrder [nCur]) {nCount ++; // nCount records the number of nodes in the left subtree} else {break ;}} assert (nCur> = nInStart & nCur <= nInEnd); // create the node BiTreeNode * pRoot = new BiTreeNode; pRoot-> m_nData = nRootDate; // according to the ordinal sequence, divide two sequences and perform recursive processing. PRoot-> m_pLeftChild = values (preOrder, nPreStart + 1, nPreStart + nCount, inOrder, nInStart, nInStart + nCount-1); pRoot-> m_pRightChild = CreateBiTreeByPreorderAndInorder (preOrder, nPreStart + nCount + 1, nPreEnd, inOrder, nInStart + nCount + 1, nInEnd); return pRoot ;} // re-build the binary tree BiTreeNode * CreateBiTreeByPreorderAndInorder (int * preOrder, int * inOrder, int nLe Ngth) {if (preOrder! = NULL) & (inOrder! = NULL) & (nLength> 0) {return CreateBiTreeByPreorderAndInorder (preOrder, 0, nLength-1, inOrder, 0, nLength-1);} else {return NULL ;}} void PreOrderPrint (BiTreeNode * pRoot) {if (pRoot! = NULL) {cout <pRoot-> m_nData <""; PreOrderPrint (pRoot-> m_pLeftChild); PreOrderPrint (pRoot-> m_pRightChild );}} void InOrderPrint (BiTreeNode * pRoot) {if (pRoot! = NULL) {InOrderPrint (pRoot-> m_pLeftChild); cout <pRoot-> m_nData <""; InOrderPrint (pRoot-> m_pRightChild );}} int _ tmain (int argc, _ TCHAR * argv []) {int nPreOrderArr [8] = {,}; int nInOrderArr [8] =, 2, 1, 5, 3, 8, 6}; BiTreeNode * pRoot = CreateBiTreeByPreorderAndInorder (nPreOrderArr, nInOrderArr, 8); cout <"FIFO sequence:"; PreOrderPrint (pRoot); cout <; cout <"post sequence:"; InOrderPrint (PRoot); cout <endl; system ("pause"); return 0 ;}# include "stdafx. h "# include <iostream> # include <assert. h> using namespace std; struct BiTreeNode {int m_nData; BiTreeNode * m_pLeftChild; BiTreeNode * m_pRightChild;}; BiTreeNode * substring (int * preOrder, int nPreStart, int nPreEnd, int * inOrder, int nInStart, int nInEnd) {// exit if (nPreStart> nPreEnd) {return NULL;} // locate the root node int n according to the first sequence RootDate = preOrder [nPreStart]; // find the root node int nCount = 0 in the middle sequence; int nCur = 0; for (nCur = nInStart; nCur <= nInEnd; nCur ++) {if (nRootDate! = InOrder [nCur]) {nCount ++; // nCount records the number of nodes in the left subtree} else {break ;}} assert (nCur> = nInStart & nCur <= nInEnd); // create the node BiTreeNode * pRoot = new BiTreeNode; pRoot-> m_nData = nRootDate; // according to the ordinal sequence, divide two sequences and perform recursive processing. PRoot-> m_pLeftChild = values (preOrder, nPreStart + 1, nPreStart + nCount, inOrder, nInStart, nInStart + nCount-1); pRoot-> m_pRightChild = CreateBiTreeByPreorderAndInorder (preOrder, nPreStart + nCount + 1, nPreEnd, inOrder, nInStart + nCount + 1, nInEnd); return pRoot ;} // rebuild the binary tree BiTreeNode * CreateBiTreeByPreorderAndInorder (int * preOrder, int * inOrder, int nLength ){ If (preOrder! = NULL) & (inOrder! = NULL) & (nLength> 0) {return CreateBiTreeByPreorderAndInorder (preOrder, 0, nLength-1, inOrder, 0, nLength-1);} else {return NULL ;}} void PreOrderPrint (BiTreeNode * pRoot) {if (pRoot! = NULL) {cout <pRoot-> m_nData <""; PreOrderPrint (pRoot-> m_pLeftChild); PreOrderPrint (pRoot-> m_pRightChild );}} void InOrderPrint (BiTreeNode * pRoot) {if (pRoot! = NULL) {InOrderPrint (pRoot-> m_pLeftChild); cout <pRoot-> m_nData <""; InOrderPrint (pRoot-> m_pRightChild );}} int _ tmain (int argc, _ TCHAR * argv []) {int nPreOrderArr [8] = {,}; int nInOrderArr [8] =, 2, 1, 5, 3, 8, 6}; BiTreeNode * pRoot = CreateBiTreeByPreorderAndInorder (nPreOrderArr, nInOrderArr, 8); cout <"FIFO sequence:"; PreOrderPrint (pRoot); cout <; cout <"post sequence:"; InOrderPrint (pRoot); cout <endl; system ("pause"); return 0 ;}

 

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.