LeetCode(Construct Binary Tree from Preorder and Inorder Traversal )根据二叉树的中序遍历和后序遍历重建二叉树
来源:互联网 发布:java怎么判断质数 编辑:程序博客网 时间:2024/06/05 06:03
题目要求:
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, vector<int> &inorder) { if (!preorder.size() || !inorder.size() ||preorder.size() != inorder.size() ) { return NULL; } TreeNode* root = constructTree(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1); return root; } TreeNode* constructTree(vector<int>& preorder, int pre_start, int pre_end, vector<int>& inorder, int in_start, int in_end) { int root_value = preorder[pre_start]; TreeNode* root = new TreeNode(root_value); if(pre_start == pre_end) { if(in_start == in_end && preorder[pre_start] == inorder[in_start]) return root; else { throw ("Invalid Input."); } } int root_in_idx = in_start; while (root_in_idx <= in_end && inorder[root_in_idx] != root_value) { ++root_in_idx; } if(root_in_idx == in_end && inorder[root_in_idx] != root_value) throw ("Invalid Input."); int left_len = root_in_idx - in_start; int left_pre_end = pre_start + left_len; if(left_len > 0) root->left = constructTree(preorder, pre_start + 1, left_pre_end, inorder, in_start, root_in_idx - 1); if(left_len < (pre_end - pre_start)) root->right = constructTree(preorder, left_pre_end + 1, pre_end, inorder, root_in_idx + 1, in_end); return root; }
0 0
- LeetCode(Construct Binary Tree from Preorder and Inorder Traversal )根据二叉树的中序遍历和后序遍历重建二叉树
- Construct Binary Tree from Preorder and Inorder Traversal 根据前序遍历和中序遍历构造二叉树
- leetCode 105.Construct Binary Tree from Preorder and Inorder Traversal (根据前序遍历和中序遍历构造二叉树)
- 【LeetCode】Construct Binary Tree From Inorder And Postorder Traversal 中序遍历和后序遍历树构造二叉树
- Construct Binary Tree from Preorder and Inorder Traversal 前序和中序重建二叉树
- 根据前序和中序序列重建二叉树 Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode - Tree - 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历重构二叉树)
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序遍历二叉树
- [LintCode] 前序遍历和中序遍历树构造二叉树 Construct Binary Tree from Preorder and Inorder Traversal
- leetCode 106.Construct Binary Tree from Inorder and Postorder Traversal (根据中序遍历和后序遍历构造二叉树)
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构造二叉树)
- Construct Binary Tree from Preorder and Inorder Traversal 从先序和中序重建二叉树@LeetCode
- Construct Binary Tree from Preorder and Inorder Traversal 前序和中序遍历构建二叉树
- 【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal 根据先序序列和中序序列恢复二叉树
- [leetcode]Construct Binary Tree from Preorder and Inorder Traversal(根据前序、中序遍历确定一棵二叉树 C语言)
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal(从前序遍历和中序遍历构造二叉树)
- 根据中序和后序序列重建二叉树 Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序重建二叉树
- 【opencv】轮廓提取
- 南阳理工学院计算机学院ACM队成员获奖情况[荣誉记]
- 携程——括号匹配(记忆化搜索)
- Hibernate复合主键查询
- linux c进程与android进程间通信
- LeetCode(Construct Binary Tree from Preorder and Inorder Traversal )根据二叉树的中序遍历和后序遍历重建二叉树
- eclipse中加入jad插件的方法
- 日期处理
- 【我所认知的BIOS】—> uEFI AHCI Driver(3) — 无处不在的protocol
- android开发--友盟初始化错误,找不到类
- 职场“潜”规则:买火车票精神 与 主观能动性
- Spring Data jpa Repository
- O2O军团混战,百度糯米搅局令对手猝不及防
- 河北安平法院院长涉嫌雇凶杀害情妇抛尸被刑拘