码迷,mamicode.com
首页 > 其他好文 > 详细

利用二叉树的先序和中序(中序和后序)排列构建二叉树

时间:2015-06-19 01:34:03      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

题目来自于:

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

这一题目其实我想说的还不是我的代码,是之前在写代码中遇到的一个bug问题。后面会进行详细的解释

Construct Binary Tree from Preorder and Inorder Traversal

 Total Accepted: 35628 Total Submissions: 134968My Submissions

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 Submissions

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);
    }
};




利用二叉树的先序和中序(中序和后序)排列构建二叉树

标签:

原文地址:http://blog.csdn.net/zhouyelihua/article/details/46554471

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!