Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

For example, given

  1. inorder = [9,3,15,20,7]
  2. postorder = [9,15,7,20,3]

Return the following binary tree:

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7
  6.  
  7. -------------------------------------------------------------------------------------
    就是从中序遍历和后序遍历构建二叉树。可以用递归方式。注意递归的终止条件。
    leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal几乎一样。
  8.  
  9. 参考博客:http://www.cnblogs.com/grandyang/p/4296193.html
  10.  
  11. C++代码:
  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. class Solution {
  11. public:
  12. TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
  13. return build(inorder,,inorder.size()-,postorder,,postorder.size() - );
  14. }
  15. TreeNode *build(vector<int> &inorder,int ileft,int iright,vector<int> &postorder,int pleft,int pright){
  16. if(ileft > iright ||pleft > pright) return NULL; //终止条件,就是当序列的长度为0时,递归终止。
  17. int i = ;
  18. TreeNode *cur = new TreeNode(postorder[pright]);
  19. for(i = ileft; i < inorder.size(); i++){
  20. if(inorder[i] == cur->val)
  21. break;
  22. }
  23. cur->left = build(inorder,ileft,i-,postorder,pleft,pleft + i - ileft - );
  24. cur->right = build(inorder,i + ,iright,postorder,pleft + i - ileft,pright - );
  25. return cur;
  26. }
  27. };

还有一个方法,就是建立几个数组,保存分割后的数组。不过时间会很长。

C++代码:

  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. class Solution {
  11. public:
  12. TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
  13. return build(inorder,postorder);
  14. }
  15. TreeNode *build(vector<int> &inorder,vector<int> &postorder){
  16. if(inorder.size() == || postorder.size() == ) //递归条件,当然也可以加上if(inorder.size() == 1 || postorder.size() == 1)return cur;这个递归条件。
  17. return NULL;
  18. int rootval = postorder.back();
  19. TreeNode *cur = new TreeNode(rootval);
  20. int i = ;
  21. for(i = ; i < inorder.size(); i++){
  22. if(inorder[i] == rootval) break;
  23. }
  24. vector<int> inleft,inright;
  25. vector<int> poleft,poright;
  26. for(int j = ; j < i; j++){
  27. inleft.push_back(inorder[j]);
  28. poleft.push_back(postorder[j]);
  29. }
  30. for(int j = i + ; j < inorder.size(); j++){
  31. inright.push_back(inorder[j]);
  32. }
  33. for(int j = i; j < postorder.size() - ; j++){
  34. poright.push_back(postorder[j]);
  35. }
  36. cur->left = build(inleft,poleft);
  37. cur->right = build(inright,poright);
  38. return cur;
  39. }
  40. };

这两个方法从算法上看是一样的,只是代码的实现不同而已。

(二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

  1. Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...

  2. [LeetCode] 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 that ...

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

  4. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

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

  5. C#解leetcode 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 that ...

  6. (二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

  7. Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal

    原题地址 二叉树基本操作 [       ]O[              ] [       ][              ]O 代码: TreeNode *restore(vector<i ...

  8. [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)

    原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...

  9. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

随机推荐

  1. ReSharper导致Visual Studio缓慢?

    问题排查 我们会竭尽所能的ReSharper的性能方面,但是也有一些已知和未知的情况下,ReSharper的可以减缓的Visual Studio. 这里有一些关键点进行故障排除和修复ReSharper ...

  2. Cs231n-assignment 1作业笔记

    KNN assignment1 KNN讲解参见: https://blog.csdn.net/u014485485/article/details/79433514?utm_source=blogxg ...

  3. python+selenium 输出2种样式的测试报告

    第一种: 1.通过 HTMLTestRunner 模块输出报告 2.下载连接 http://tungwaiyip.info/software/HTMLTestRunner.html 3.将下载好的文件 ...

  4. Java操作Excel(使用POI)

    背景说明 以前写过使用 JXL 操作Excel的例子,但JXL对于Excel 2007版本以后的文件(即扩展名为 .xlsx)无法读取,也找不到可以支持的包.所以,有时不得不用 POI 来操作Exce ...

  5. C# 里面swith的或者

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  6. 【转】在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...

  7. windows下搭建nginx+php+laravel开发环境(转)

    1.前言 windows下大多我们都是下载使用集成环境,但是本地已经存在一个集成环境,但不适合项目的需求.因此准备再自己搭建一个环境. 2.准备 工具: 1) 下载 nginx1.14.0(版本根据自 ...

  8. JS正则四个反斜杠的含义

    我们首先来看如下代码,在浏览器中输出的是什么? // 在浏览器中输出的 console.log('\\'); // 输出 \ console.log('\\\\'); // 输出 \\ 一:js正则直 ...

  9. 一文搞懂Raft算法

      raft是工程上使用较为广泛的强一致性.去中心化.高可用的分布式协议.在这里强调了是在工程上,因为在学术理论界,最耀眼的还是大名鼎鼎的Paxos.但Paxos是:少数真正理解的人觉得简单,尚未理解 ...

  10. 即将发布的 ASP.NET Core 2.2 会有哪些新玩意儿?

    今年 6 月份的时候时候 .NET 团队就在 GitHub 公布了 ASP.NET Core 2.2 版本的 Roadmap(文末有链接),而前两天 ASP.NET Core 2.2 预览版 2 已经 ...