106. Construct Binary Tree from Inorder and Postorder Traversal
2016-05-28 18:22
405 查看
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
2.对以上的t->right 和t->left做了修改
在上面,我直接将pos代替pe-(ie-pos),将pos-1代替pe-(ie-pos)-1,这个犯了很大的错误,就是默认pe和ie的位置是一样的,这是非常不正确的,牢记!!!
3.另一种方法没去看,先贴代码,java的,以下是别人的解释:
The core idea is: Starting from the last element of the postorder and inorder array, we put elements
from postorder array to a stack and each one is the right child of the last one until an element in postorder array is equal to the element on the inorder array. Then, we pop as many as elements we can from the stack and decrease the mark in inorder array
until the peek() element is not equal to the mark value or the stack is empty. Then, the new element that we are gonna scan from postorder array is the left child of the last element we have popped out from the stack.
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder.length == 0 || postorder.length == 0) return null;
int ip = inorder.length - 1;
int pp = postorder.length - 1;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode prev = null;
TreeNode root = new TreeNode(postorder[pp]);
stack.push(root);
pp--;
while (pp >= 0) {
while (!stack.isEmpty() && stack.peek().val == inorder[ip]) {
prev = stack.pop();
ip--;
}
TreeNode newNode = new TreeNode(postorder[pp]);
if (prev != null) {
prev.left = newNode;
} else if (!stack.isEmpty()) {
TreeNode currTop = stack.peek();
currTop.right = newNode;
}
stack.push(newNode);
prev = null;
pp--;
}
return root;
}
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* buildTree(vector<int>& inorder, vector<int>& postorder) { return createTree(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1); } TreeNode* createTree(vector<int>& inorder, int ib, int ie, vector<int>& postorder, int pb, int pe){ if(ie < ib || pe < pb) return NULL; TreeNode* t = new TreeNode(postorder.at(pe)); int pos = 0; for(int i = ie; i >=ib; i--){ if(inorder.at(i) == t->val){ pos = i; break; } } t->right = createTree(inorder, pos+1, ie, postorder, pos, pe-1); t->left = createTree(inorder, ib, pos-1, postorder, pb, pos-1); return t; } };
2.对以上的t->right 和t->left做了修改
在上面,我直接将pos代替pe-(ie-pos),将pos-1代替pe-(ie-pos)-1,这个犯了很大的错误,就是默认pe和ie的位置是一样的,这是非常不正确的,牢记!!!
/** * 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) { return createTree(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1); } TreeNode* createTree(vector<int>& inorder, int ib, int ie, vector<int>& postorder, int pb, int pe){ if(ie < ib || pe < pb) return NULL; TreeNode* t = new TreeNode(postorder.at(pe)); if(ie == ib || pe == pb) return t; int pos = 0; for(int i = ie; i >=ib; i--){ if(inorder.at(i) == t->val){ pos = i; break; } } t->right = createTree(inorder, pos+1, ie, postorder, pe-(ie-pos), pe-1); t->left = createTree(inorder, ib, pos-1, postorder, pb, pe-(ie-pos)-1); return t; } };
3.另一种方法没去看,先贴代码,java的,以下是别人的解释:
The core idea is: Starting from the last element of the postorder and inorder array, we put elements
from postorder array to a stack and each one is the right child of the last one until an element in postorder array is equal to the element on the inorder array. Then, we pop as many as elements we can from the stack and decrease the mark in inorder array
until the peek() element is not equal to the mark value or the stack is empty. Then, the new element that we are gonna scan from postorder array is the left child of the last element we have popped out from the stack.
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder.length == 0 || postorder.length == 0) return null;
int ip = inorder.length - 1;
int pp = postorder.length - 1;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode prev = null;
TreeNode root = new TreeNode(postorder[pp]);
stack.push(root);
pp--;
while (pp >= 0) {
while (!stack.isEmpty() && stack.peek().val == inorder[ip]) {
prev = stack.pop();
ip--;
}
TreeNode newNode = new TreeNode(postorder[pp]);
if (prev != null) {
prev.left = newNode;
} else if (!stack.isEmpty()) {
TreeNode currTop = stack.peek();
currTop.right = newNode;
}
stack.push(newNode);
prev = null;
pp--;
}
return root;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- AVL树-自平衡二叉查找树(Java实现)
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解