Description:

Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and Postorder' (Problem 106), u need build the binary tree.

Input:

105. Preorder & Inorder traversal

106. Inorder & Postorder traversal

output:

A binary tree.

Solution:

  This solution uses the algorithm "divide and conquer". Taking an example of 105, we can get the root node from preorder travelsal then use inorder traversal to divide the range of left tree nodes and the right tree nodes. You can

draw some simple instances on paper,  which will make you totally clear about the thinking.

  1. /**
  2. * Definition for a binary tree node.
  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. //
  11. class Solution {
  12. public:
  13. void travel(TreeNode **root)
  14. {
  15. if(*root){
  16. travel(& ((*root) -> left));
  17. cout<<"val is "<<(*root)->val<<endl;
  18. travel(& ((*root) -> right));
  19. }
  20. }
  21.  
  22. TreeNode* createTree(vector<int>& preorder, vector<int>& inorder, int preSta, int preEnd, int inSta, int inEnd)
  23. {
  24. if(preSta > preEnd || inSta > inEnd ) return NULL;
  25. TreeNode *root = new TreeNode(preorder[preSta]);
  26. int index;
  27. for(int i = inSta; i <= inEnd; i ++){
  28. if(inorder[i] == preorder[preSta]){
  29. index = i;
  30. break;
  31. }
  32. }
  33. root -> left = createTree(preorder, inorder, preSta + , preSta + index - inSta, inSta, index - );
  34. root -> right = createTree(preorder, inorder, preSta + index - inSta + , preEnd, index + , inEnd);
  35. return root;
  36. }
  37.  
  38. TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
  39. TreeNode *root = createTree(preorder, inorder, , preorder.size() - , , inorder.size() - );
  40. TreeNode **r = &root;
  41. //travel(r);
  42. return root;
  43. }
  44. };
  45.  
  46. //106.
  47. class Solution {
  48. public:
  49. TreeNode* createTree(vector<int>& inorder, vector<int>& postorder, int inSta, int inEnd, int postSta, int postEnd){
  50. if(inSta > inEnd || postSta > postEnd)
  51. return NULL;
  52. TreeNode* root = new TreeNode(postorder[postEnd]);
  53. int index;
  54. for(int i = inEnd; i >= inSta; i --){
  55. if(postorder[postEnd] == inorder[i]){
  56. index = i;
  57. break;
  58. }
  59. }
  60. root -> right = createTree(inorder, postorder, index + , inEnd, postEnd - (inEnd - index), postEnd - );
  61. root -> left = createTree(inorder, postorder, inSta, index - , postSta, postSta + (index - inSta - ));
  62. return root;
  63. }
  64. TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
  65. TreeNode* root = createTree(inorder, postorder, , inorder.size() - , , postorder.size() - );
  66. return root;
  67. }
  68. };

【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal的更多相关文章

  1. 【LeetCode】105 & 106. Construct Binary Tree from Inorder and Postorder Traversal

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...

  2. 105 + 106. Construct Binary Tree from Preorder and Inorder Traversal (building trees)

    Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...

  3. 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 ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

  6. 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  7. 【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  8. 【leetcode】Minimum Depth of Binary Tree

    题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

  9. 【leetcode】Minimum Depth of Binary Tree (easy)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

随机推荐

  1. Just a Hook (HDU 1698) 懒惰标记

    Just a Hook (HDU 1698) 题链 每一次都将一个区间整体进行修改,需要用到懒惰标记,懒惰标记的核心在于在查询前才更新,比如将当前点rt标记为col[rt],那么此点的左孩子和右孩子标 ...

  2. Navigator的学习

      Navigator 对象包含有关浏览器的信息.注释:没有应用于 navigator 对象的公开标准,不过所有浏览器都支持该对象.   我感觉需要看什么属性和方法,直接输出这个navigator,然 ...

  3. JSON.parseObject将json字符串转换为bean类,是否大小写敏感区分---https://blog.csdn.net/mathlpz126/article/details/80684034

    JSON.parseObject将json字符串转换为bean类,是否大小写敏感区分 https://blog.csdn.net/mathlpz126/article/details/80684034

  4. PHP中HTTP_X_FORWARDED_FOR 和 REMOTE_ADDR使用详解

    1.REMOTE_ADDR:浏览当前页面的用户计算机的ip地址 2.HTTP_X_FORWARDED_FOR: 浏览当前页面的用户计算机的网关 3.HTTP_CLIENT_IP:客户端的ip 在PHP ...

  5. Spring实现封装自定义注解@Trimmed清除字符串前后的空格

    在Spring中实现字符串清除的方法有很多,原生方法String自带trim()方法,或者使用StringUtils提供的trim...方法. 通常可以将上面的方式封装成自定义注解的形式去实现来节省更 ...

  6. ISATAP隧道方式接入IPv6和RRAS(Windows路由与远程访问)似乎是冲突的

    在启用了RRAS的NAT服务器上设置ISATAP隧道,虽然能正常获取IPv6地址,但是IPv6网络实际是不通的...

  7. PHP array_merge()

    定义和用法 array_merge() 函数把两个或多个数组合并为一个数组. 如果键名有重复,该键的键值为最后一个键名对应的值(后面的覆盖前面的).如果数组是数字索引的,则键名会以连续方式重新索引. ...

  8. Bitmap工具类BitmapHelper

    BitmapHelper 提供一些获取本地缩略图,获取网络图片.dp与px的相互转换等方法. import java.io.ByteArrayInputStream; import java.io.B ...

  9. Office 如何打印A4不干胶标签纸

    1 下载Label Expert这个软件,注意不是第一个Avery Wizard(卖家可能会送你这个软件,但是送的这款软件是简体中文版的,似乎模板不全,所以最好还是自己下,反正我最后是由于找不到对应的 ...

  10. Python中range和xrange的异同之处

    range     函数说明:range([start,] stop[, step]).依据start与stop指定的范围以及step设定的步长,生成一个序列. range演示样例:  >> ...