Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
// construct tree from preorder and inorder class Solution { public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { if(preorder.size() == 0) return NULL; return buildTree(preorder, 0, inorder, 0, preorder.size()); } TreeNode *buildTree(vector<int> &pre, int preStart, vector<int> &in, int inStart, int len) { if(preStart < 0 || preStart + len > pre.size() || inStart < 0 || inStart + len > in.size() || len < 1) { return NULL; } int rootVal = pre[preStart]; int i = 0; while(in[i] != rootVal) ++i; TreeNode *root = new TreeNode(rootVal); int leftLen = i - inStart; int rightLen = len - leftLen - 1; root->left = buildTree(pre, preStart+1, in, inStart, leftLen); root->right = buildTree(pre, preStart+leftLen+1, in, i+1, rightLen); return root; } };
欢迎关注微信公众号——计算机视觉:
相关推荐
105.construct_binary_tree_from_preorder_and_inorder_traversal从前序
Construct Binary Tree from Inorder and Postorder Traversa.根据先序后续构建二叉树
Construct Binary Tree from Preorder and Inorder Traversal Construct Binary Tree from Inorder and Postorder Traversal 二叉查找树 Unique Binary Search Trees Unique Binary Search Trees II Validate Binary...
Inorder Traversal 用两个栈实现队列 232. Implement Queue using Stacks 旋转数组的最小数字 153. Find Minimum in Rotated Sorted Array 斐波那契数列 509. Fibonacci Number 跳台阶 70. Climbing Stairs 变态跳...
421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...
105.construct-binary-tree-from-preorder-and-inorder-traversal (从前序与中序遍历序列构造二叉树) 106.construct-binary-tree-from-inorder-and-postorder-traversal (从中序与后序遍历序列构造二叉树) 112.path-...
[105_construct-binary-tree-from-preorder-and-inorder-traversal.cpp] [106_construct-binary-tree-from-inorder-and-postorder-traversal.cpp] [107_binary-tree-level-order-traversal-ii.cpp] [108_convert-...
construct-binary-tree-from-preorder-and-inorder-traversal 无官方题解 106 construct-binary-tree-from-inorder-and-postorder-traversal 无官方题解 116 populating-next-right-pointers-in-eac
Construct Optimal Binary Search Tree by Using Greedy Algorithm
to construct binary codes for users and items such that the preference of users over items can be accurately preserved by the Hamming distance between their respective binary codes. By using two loss ...
No prior experience is required.Game Development with Construct 2 teaches you to create 12 different game projects from a variety of genres, including car racing and tower defense to platformer and ...
二叉树的实现代码 /*LinkTree.h*/ typedef int DataType; //这样定义意味着节点中能放一个整数,但如果是非叶子节点则可以解读为对应的操作符的ascii ...void inOrder( BinTree t); //travel in order
https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 2 题目描述 根据一棵树的中序遍历与后序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 中序...
concerning 345 burglars and 310 violent offenders in Nottinghamshire, England. The most important observable features of offenders were sex, ethnicity, age, height, build, hair color, hair length, and...
The frequency's must be listed, in order, from largest (at the top) to smallest (at the bottom) Then, using the Huffman algorithm, construct the optimal prefix binary code for the table. The Huffman ...
内含大量实例,来自多个知名算法测试平台的题目,还有讲解。
language testing 权威刊物文章 可供语言测试与学习者使用。
A Basic Tool to Construct Basic Pourbaix and Tafe
The methodology used to construct tree structured rules is the focus of this monograph. Unlike many other statistical procedures, which moved from pencil and paper to calculators, this text's use of ...