106. Construct Binary Tree from Inorder and Postorder Traversal
2016-05-19 10:54
375 查看
题目:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
题意:
给定中序遍历跟后序遍历构建完整二叉树
note:
可以假设该二叉树中没有重复元素
思路一:
与106. Construct Binary Tree from Preorder and inorder Traversal题目类似,采用递归的方法实现二叉树的构建。
例子:前序遍历序列:1 2 4 5 3 6 7
中序遍历序列:4 2 5 1 6
3 7
后序遍历序列:4 5 26 7 31
代码:40ms
与106.
Construct Binary Tree from Preorder and inorder Traversal题目类似,采用堆栈的方法实现二叉树的构建。
代码:16ms
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
题意:
给定中序遍历跟后序遍历构建完整二叉树
note:
可以假设该二叉树中没有重复元素
思路一:
与106. Construct Binary Tree from Preorder and inorder Traversal题目类似,采用递归的方法实现二叉树的构建。
例子:前序遍历序列:1 2 4 5 3 6 7
中序遍历序列:4 2 5 1 6
3 7
后序遍历序列:4 5 26 7 31
代码:40ms
<span style="background-color: rgb(246, 246, 246);">/** * 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* buildTree(vector<int>& inorder, vector<int>& postorder) { int inorderSize = inorder.size(); int postorderSize = postorder.size(); if (inorderSize == 0 || inorderSize != postorderSize) return NULL; TreeNode *root = recursionBuild(inorder, 0, inorderSize-1, postorder, 0, postorderSize-1); return root; } TreeNode* recursionBuild(vector<int>& inorder, int inLeft, int inRight, vector<int>& postorder, int postLeft, int postRight) { if (inLeft > inRight) return NULL; int i = 0; TreeNode *node = new TreeNode(postorder[postRight]); for (i = inLeft; i <= inRight; i++) { if (inorder[i] == node->val) break; } node->left = recursionBuild(inorder, inLeft, i-1, postorder, postLeft, postLeft+i-inLeft-1); node->right = recursionBuild(inorder, i+1, inRight, postorder, postLeft+i-inLeft, postRight-1); return node; } }; </span>思路二:
与106.
Construct Binary Tree from Preorder and inorder Traversal题目类似,采用堆栈的方法实现二叉树的构建。
代码:16ms
/** * 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* buildTree(vector<int>& inorder, vector<int>& postorder) { if(postorder.empty()){ return nullptr; } int size = postorder.size(); TreeNode* root = new TreeNode(postorder[size-1]); TreeNode* cur = root; stack<TreeNode*> stacks; stacks.push(root); int flag = 1; int post_index = size-2; int in_index = size-1; while(post_index>=0){ if(!stacks.empty() && stacks.top()->val==inorder[in_index]){ cur = stacks.top(); flag = 0; stacks.pop(); in_index--; }else{ TreeNode* tmp = new TreeNode(postorder[post_index]); if(flag==1){ cur->right = tmp; cur = cur->right; }else{ cur->left = tmp; cur = cur->left; flag = 1; } stacks.push(tmp); post_index--; } } return root; } };
相关文章推荐
- phantomjs安装使用
- perl 调用方法 子例程说明
- perl 调用方法 子例程说明
- 关于c#短信发送接口使用说明
- shell脚本学习笔记(2)
- 远程主机强迫关闭了一个现有的连接
- 假设检验中的P值 与显著性水平的联系
- [置顶] Matlab R2014b配置vlfeat0.9.20
- linux tomcat跟随系统自动启动
- ZooKeeper 安装部署及hello world
- ubuntu 12.04 桌面版关闭图形界面
- 网络基础知识
- Android 使用Picasso加载网络图片等比例缩放
- 《招聘一个靠谱的 iOS》—参考答案(下)
- load,initialize,initWithCoder,awakeFromNib,initWithFrame,layoutSubviews 个人总结
- 23种设计模式(11)--Facade模式
- java导入导出excel 01
- 去除html代码里面的script正则方法
- 批次动销查询去物流
- 49. Group Anagrams