LeetCode OJ - Construct Binary Tree from Preorder and Inorder Traversal
题目:
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
解题思路:
前序遍历序列的第一个元素肯定为根节点;然后再在中序遍历序列中找到该节点,并以此将中序遍历序列分为左右两部分,然后对左右两部分分别进行递归求解。
代码:
1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int findIndex(int target, vector<int> &inorder, int begin, int end) { 13 for (int i = begin; i <= end; i++) { 14 if (inorder[i] == target) { 15 return i; 16 } 17 } 18 return -1; 19 } 20 TreeNode *buildTreeCore(vector<int> &preorder, int pre_begin, int pre_end, vector<int> &inorder, int in_begin, int in_end) { 21 if (pre_begin > pre_end || in_begin > in_end) return NULL; 22 23 TreeNode *root = new TreeNode(preorder[pre_begin]); 24 if (pre_begin == pre_end) { 25 return root; 26 } else { 27 int index = findIndex(preorder[pre_begin], inorder,in_begin, in_end); 28 int left_nodes = index - in_begin; 29 TreeNode *left_root = buildTreeCore(preorder, pre_begin + 1, pre_begin + left_nodes, inorder, in_begin, index - 1); 30 TreeNode *right_root = buildTreeCore(preorder, pre_begin + left_nodes + 1, pre_end, inorder, index + 1, in_end); 31 root->left = left_root; 32 root->right = right_root; 33 return root; 34 } 35 } 36 TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { 37 TreeNode *root = buildTreeCore(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1); 38 return root; 39 } 40 };
转载于:https://www.cnblogs.com/dongguangqing/p/3728246.html
LeetCode OJ - Construct Binary Tree from Preorder and Inorder Traversal相关推荐
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 本博客转载自:http://www.cnblogs.co ...
- [LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal
题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...
- leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)...
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...
- LeetCode: 105. Construct Binary Tree from Preorder and Inorder Traversal
题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal-前序中序遍历构造二叉树-Python和Java递归解法
题目地址:Construct Binary Tree from Preorder and Inorder Traversal - LeetCode Given preorder and inorder ...
- LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
- [leetcode] Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal(从前序以及中序遍历结果中构造二叉树)...
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...
最新文章
- iOS开发线程同步技术-锁
- idea下org.apache.commons.dbcp.BasicDataSourc找不到
- 使用eclipse快速set/get
- imu_utils标定imu问题解决
- 解释为脑瘫的那张图_Python GIL全局解释器锁详解(深度剖析)
- 【转】ASP.NET MVC 3 Service Location, Part 5: IDependencyResolver
- HDU 4336 Card Collector(状压 + 概率DP 期望)题解
- ALSA之PCM分析
- 让代码在SharePoint页面执行如何在aspx页面中写代码
- linux象棋软件下载,一个中国象棋游戏
- 常用的台式计算机,台式电脑常见简单故障排除
- 听说crmeb多商户增加了种草功能?
- 大容量sd卡reread之后/dev下概率性出现无设备文件
- 同方知网阅读器_汪总对峙众粉丝!透露为何加入凌烟阁?澄清塌哥,汪总平台号 一个办公室?大姐刺激汪总退网?...
- Android锁屏下弹窗的尝试,android开发实战我的云音乐
- crt、cer类型证书转换成bks
- ubuntu14.04安装Kile
- CGB2108day17
- 鹏业四川CJZ整体解决方案
- java计算机毕业设计技术旅游平台源码+mysql数据库+系统+lw文档+部署
热门文章
- WES 软件安装 及Bundel数据的下载
- BIRCH算法(Java实现)
- 全局系统性地把握客户感知-建立VOC
- java中引用类型作形参_阿花宝宝 Java基础笔记 之 引用类型作为参数
- ping的时候怎么暂停_dos命令pause教程,?暂停bat批处理脚本程序,?请按任意键继续...
- ffmpeg命令_温故知新:ffmpeg操作《天空之城》。窗口党勿入,都是指令!
- PAT (Basic Level) Practice1007 素数对猜想
- 衡量失败检测算法的指标
- grafana入门学习
- 基于Stm32F746g_disg平台下移植zephry使用TinyML预测模型