利用二叉樹的先序和中序(中序和後序)排列構建二叉樹,二叉樹

來源:互聯網
上載者:User

利用二叉樹的先序和中序(中序和後序)排列構建二叉樹,二叉樹

題目來自於:

https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/

這一題目其實我想說的還不是My Code,是之前在寫代碼中遇到的一個bug問題。後面會進行詳細的解釋

Construct Binary Tree from Preorder and Inorder Traversal Total Accepted: 35628 Total Submissions: 134968My SubmissionsQuestion Solution 

Given preorder and inorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:TreeNode* buildhelp(vector<int>& preorder, vector<int>& inorder,int pStart,int pEnd,int iStart,int iEnd){      if(pStart>pEnd||iStart>iEnd)      return NULL;      TreeNode *Tnode=new TreeNode(preorder[pStart]);      if(pStart==pEnd)      return Tnode;      int cur=iStart;      while(inorder[cur]!=preorder[pStart])          cur++;  Tnode->left=buildhelp(preorder, inorder,pStart+1,pStart+(cur-iStart),iStart,cur-1);  Tnode->right=buildhelp(preorder, inorder,pEnd-(iEnd-cur-1),pEnd,cur+1,iEnd);      return Tnode;  }    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {        TreeNode* Tree=NULL;        if(preorder.size()==0)         return Tree;        Tree=buildhelp(preorder, inorder,0,preorder.size()-1,0,inorder.size()-1);        return Tree;    }};

上述代碼是正確的可以AC的,但是本人在第一次寫的時候寫成了

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:TreeNode* buildhelp(vector<int>& preorder, vector<int>& inorder,int pStart,int pEnd,int iStart,int iEnd){      if(pStart<pEnd||iStart<iEnd)      return NULL;      TreeNode Tnode(preorder[pStart]);      if(pStart==pEnd)      return &Tnode;      int cur=iStart;      while(inorder[cur]!=preorder[pStart])          cur++;  Tnode.left=buildhelp(preorder, inorder,pStart+1,pStart+(cur-iStart),iStart,cur-1);  Tnode.right=buildhelp(preorder, inorder,pEnd-(iEnd-cur-1),pEnd,cur+1,iEnd);      return &Tnode;  }    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {        TreeNode* Tree=NULL;        if(preorder.size()==0)         return Tree;        Tree=buildhelp(preorder, inorder,0,preorder.size()-1,0,inorder.size()-1);        return Tree;    }};

不同的地方在於

  TreeNode Tnode(preorder[pStart]);
然後返回其指標

這裡犯了一個很大的錯誤就是,其實每次演算法在遇到該處時候只是將結構體中的成員變數替換成了新的,卻沒有構建新的節點。

為了驗證我們的想法:

#include<iostream>#include<vector>using namespace std; struct TreeNode {     int val;     TreeNode *left;     TreeNode *right;      TreeNode(int x) : val(x), left(NULL), right(NULL) {}  };int main(){int i=5; while(i--) {  TreeNode aa(i);  cout<<&aa<<endl; }}
我們在一個while中多次重建該結構體但是其地址都沒有發生改變



最後是第二道類似的演算法

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 32565 Total Submissions: 121341My SubmissionsQuestion Solution 

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:TreeNode* buildhelp(vector<int>& inorder, vector<int>& postorder,int iStart,int iEnd,int pStart,int pEnd){      if(pStart>pEnd||iStart>iEnd)      return NULL;      TreeNode Tnode(postorder[pEnd]);      if(pStart==pEnd)      return &Tnode;      int cur=iStart;      while(inorder[cur]!=postorder[pEnd])          cur++;  Tnode.left=buildhelp(inorder, postorder,iStart,cur-1,pStart,pStart+(cur-iStart-1));  Tnode.right=buildhelp(inorder, postorder,cur+1,iEnd,pEnd-1-(iEnd-cur-1),pEnd-1);      return &Tnode;  }    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {         if(inorder.size()==0)         return NULL;        return buildhelp(inorder, postorder,0,inorder.size()-1,0,postorder.size()-1);    }};




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.