一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

来源:https://leetcode.com/problems/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 duplicates do not exist in the tree

(二)解题

本题大意:给定一个二叉树的中序和后序遍历,构造出该二叉树

思路可以参考:【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal

与上题一样,只不过,后序遍历的最后一个节点为根节点,然后在中序遍历中找到根节点,从而找出根节点的左右子树。

中序遍历为:左子树+根节点+右子树

后序遍历为:左子树+右子树+根节点

例如:中序遍历213,后序遍历231,根节点为1,在中序遍历中确定2为左子树,3为右子树。

具体思路见代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if(inorder.empty()||postorder.empty()) return NULL;//为空则返回NULL
        return constructTree(inorder,postorder,0,inorder.size()-1,0,postorder.size()-1);
    }
    TreeNode* constructTree(vector<int>& inorder, vector<int>& postorder, int inStart,int inEnd,int postStart,int postEnd)
    {
        if(postStart>postEnd||inStart>inEnd) return NULL;
        TreeNode* root = new TreeNode(postorder[postEnd]);//根节点为后序遍历的
        if(postStart==postEnd||inStart==inEnd) return root;
        int i ;
        for(i = inStart ;i<inEnd;i++)//在中序遍历中找到根节点
        {
            if(inorder[i]==postorder[postEnd]) break;
        }
        root->left = constructTree(inorder,postorder,inStart,i-1,postStart,postStart+i-inStart-1);//构造左子树
        root->right = constructTree(inorder,postorder,i+1,inEnd,postStart+i-inStart,postEnd-1);//构造右子树
        return root;
    }
};

【一天一道LeetCode】#106. Construct Binary Tree from Inorder and Postorder Traversall的更多相关文章

  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 (用中序和后序树遍历来建立二叉树)

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

  5. leetcode 106 Construct Binary Tree from Inorder and Postorder Traversal----- java

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

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

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

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

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

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

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

随机推荐

  1. jquery form提交

    $('#form').form("submit", { url: "Stu_sendsms.aspx", onSubmit: function () { $(' ...

  2. Mysql根据一个基库生成其他库与其不同的库升级脚本

    今天研究了一下不同数据库之间如何做同步.弄了一个升级工具类,希望以后还能有所帮助. public class UpgradeDataBase { public static void main(Str ...

  3. 模仿天猫实战【SSM版】——项目起步

    前言:现在自己的学习似乎遇到了瓶颈,感觉学习了 SSM 之后有一些迷茫,不知道接下来该往哪里去努力了,我觉得这是个很不好的状态,为了度过这段时期,我准备把天猫模仿下来(给自己找点事做)之后开始去巩固 ...

  4. 02 基础设施/Gitlab - DevOps之路

    基础设施/Gitlab - DevOps之路 文章Github地址,欢迎start:https://github.com/li-keli/DevOps-WiKi 源代码管理器选用Gitlab,新版(2 ...

  5. c++ Lambda表达式待修改

    C++11引入了lambda表达式,使得程序员可以定义匿名函数,该函数是一次性执行的,既方便了编程,又能防止别人的访问. Lambda表达式的语法通过下图来介绍: 这里假设我们定义了一个如上图的lam ...

  6. 如果将Joomla网站搜索结果显示到一个“干净”页面

    有时候大家会发现Joomla网站自带的或者第三方的搜索功能时,搜索结果会显示在首页,和首页其它的模块如图片橱窗等显示在一起,非常混乱. 在这里教大家一个不需要修改代码的小技巧来解决这个问题,使搜索结果 ...

  7. sshpass笔记

    sshpass简介 ssh登录的时候使用的是交互式输入,不能预先在命令行使用参数指定密码,sshpass就是为了解决这个问题的.sshpass提供非交互式输入密码的方式,可以用在shell脚本中自动输 ...

  8. Java中使用CountDownLatch进行多线程同步

    CountDownLatch介绍 在前面的Java学习笔记中,总结了Java中进行多线程同步的几个方法: 1.synchronized关键字进行同步. 2.Lock锁接口及其实现类ReentrantL ...

  9. mybatis映射器配置细则

    前面三篇博客我们已经多次涉及到映射器的使用了,增删查基本上都用过一遍了,但是之前我们只是介绍了基本用法,实际上mybatis中映射器可以配置的地方还是非常多,今天我们就先来看看映射器还有哪些需要配置的 ...

  10. 教你如何在Android 6.0上创建系统悬浮窗

    郭霖大神的文章:http://mp.weixin.qq.com/s?__biz=MzA5MzI3NjE2MA==&mid=2650235949&idx=1&sn=0f7eded ...