构建二叉树(前中序构建105,中后序构建106)--非递归的方法值得研究
来源:互联网 发布:ubuntu vsftpd配置 编辑:程序博客网 时间:2024/06/07 09:44
一、前中序构建
Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
题目大意:根据二叉树的前序遍历和中序遍历重新构建一颗二叉树
方法一、递归的方法构建
思路:由于前序遍历的第一个数字为根节点的数字,所以构建根节点,然后去中序遍历中找到根节点的位置,从而定位出根节点的左子树所拥有的孩子节点的数目,以及根节点的右子树的节点数,从而递归的构建左右子树
TreeNode* buildTree(vector<int>& preorder,int begin1,int end1,vector<int>& inorder,int begin2,int end2) { if(begin1 > end1) return NULL; else if (begin1 == end1) return new TreeNode(preorder[begin1]); TreeNode* root = new TreeNode(preorder[begin1]); int i = begin2; for(; i<= end2; i++) { if(inorder[i] == preorder[begin1]) { break; } } int leftLen = i - begin2; root->left = buildTree(preorder,begin1+1,begin1+leftLen,inorder,begin2,begin2+leftLen-1); root->right = buildTree(preorder,begin1+leftLen+1,end1,inorder,begin2+leftLen+1,end2); return root; } TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { return buildTree(preorder,0,preorder.size()-1,inorder,0,inorder.size()-1); }
方法二、非递归遍历
巧用栈–可以研究研究
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { TreeNode* root = NULL; if(preorder.size() == 0) return root; stack<TreeNode*> sta; root = new TreeNode(preorder[0]); sta.push(root); int index=0; for(int i = 1; i < preorder.size(); i++) { TreeNode* cur = sta.top(); if(sta.top()->val != inorder[index]) { cur->left = new TreeNode(preorder[i]); sta.push(cur->left); } else { while(!sta.empty() && sta.top()->val == inorder[index]) { cur = sta.top(); sta.pop(); index++; } if(index < inorder.size()) { cur->right = new TreeNode(preorder[i]); sta.push(cur->right); } } } return root; }
二、中后序构建
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
方法一、递归的方法
思想和上述差不多
TreeNode* buildTree(vector<int>& inorder,int begin1, int end1, vector<int>& postorder,int begin2, int end2) { if(begin2 > end2) return NULL; else if(begin2 == end2) return new TreeNode(postorder[end2]); TreeNode* root = new TreeNode(postorder[end2]); int i = begin1; for(; i<= end1; i++) { if(inorder[i] == postorder[end2]) break; } int leftLen = i - begin1; root->left = buildTree(inorder,begin1,begin1+leftLen-1,postorder,begin2,begin2+leftLen-1); root->right = buildTree(inorder,begin1+leftLen+1,end1,postorder,begin2+leftLen,end2-1); //注意由于end2处为根节点,所以过滤掉 return root; } TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { return buildTree(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1); }
方法二:非递归的实现
代码见:https://discuss.leetcode.com/topic/4746/my-comprehension-of-o-n-solution-from-hongzhi
- 构建二叉树(前中序构建105,中后序构建106)--非递归的方法值得研究
- 用递归的方法构建二叉树
- c原生实现二叉树的构建和 非递归遍历二叉树的方法
- 重现二叉树非递归算法的构建过程
- 树的学习——(递归构建二叉树、递归非递归前序中序后序遍历二叉树、根据前序序列、中序序列构建二叉树)
- 递归构建二叉树---中序遍历二叉树(递归与非递归)
- 二叉树的建立(根据遍历结果构建)、遍历(非递归)和搜索
- 构建二叉树(据后序遍历序列)---后续遍历二叉树(递归与非递归)
- 构建二叉排序树(非递归)
- C++ 二叉树的构建,先序/中序/后序的递归/非递归实现
- 二叉树(利用栈和队列实现递归和非递归遍历,构建等)
- 二叉树构建以及前中后序遍历(递归和非递归)Java实现
- 二叉树的构建
- 二叉树的构建
- 某国内IT大牌名企校招笔试 + 树的学习—(递归构建二叉树、递归非递归前序中序后序遍历二叉树、根据前序序列、中序序列构建二叉树)
- 二叉树递归和非递归遍历以及层次构建节点数为n的二叉树
- 构建二叉树(据前序遍历结果)--- 前序遍历二叉树(递归与非递归)
- java 实现二叉树的构建,先序,中序,后序,层次,递归,非递归的遍历
- scala sealed关键字
- ti图形库
- C/C++常见编译错误整理
- centos安装kubernets(三) 解决dashboard报错解决
- Oauth2.0和Oauth1.0的区别
- 构建二叉树(前中序构建105,中后序构建106)--非递归的方法值得研究
- CentOS下安装JDK的三种方法
- WPF中WrapPanel、StackPanel等添加滚动条ScrollViewer
- nodejs开发1创建新的app
- 北京理工大学抢课教程
- VS中包含目录、附加包含目录、附加库目录及附加依赖项
- java 如何读取jar包外的properties文件(转)
- Android socket 编程 实现消息推送
- Xshell的正确使用方式