LeetCode Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:死而后已不亦远乎 编辑:程序博客网 时间:2024/06/15 08:01
题目:
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 binary tree * 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) { TreeNode *root = createTree(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1); return root; } TreeNode *createTree(vector<int> &inorder, int inBeg, int inEnd, vector<int> &postorder, int postBeg, int postEnd) { if(postBeg > postEnd) return NULL; int rootVal = postorder[postEnd]; int index; for(int i = inBeg; i <= inEnd; i++) { if(inorder[i] == rootVal) { index = i; break; } } int len = index-inBeg; TreeNode *root = new TreeNode(rootVal); root->left = createTree(inorder, inBeg, index-1, postorder, postBeg, postBeg+len-1); root->right = createTree(inorder, index+1, inEnd, postorder, postBeg+len, postEnd-1); return root; }};
0 0
- *(leetcode) Construct Binary Tree from Inorder and Postorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 110: Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode-Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode - Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- Objective-C语法property详解
- 权限带来的很二事情
- KVO模式
- 100个数,范围是0(1)~99 ,其中有一个重复,求重复的数字。
- 位运算知识整理
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- 重复字符统计算法
- 数组在程序设计中,为了处理方便, 把具有相同类型的若干变量按有序的形式组织起来。这些按序排列的同类数据元素的集合称为数组
- intelliJ IDEA 10的使用简要
- itoa 库函数的实现
- XMLHttpRequest和ActiveXObject学习
- Pocketsphinx语音识别--使用流程, 安装,使用
- 关于OnPaint, OnEraseBkGnd, WM_PAINT,WM_ERASEBKGND
- asio学习之三:asio实现简单的服务器和客户端