Construct Binary Tree from Inorder and Postorder Traversal 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/


Description

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

Note:

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

Solution

class Solution {
public:
TreeNode* getTreeNode(vector<int>& inOrder, vector<int>& postOrder,
int inStart, int inEnd, int postStart, int postEnd) {
TreeNode* resultNode = new TreeNode(postOrder[postEnd]);
if (postStart == postEnd)
return resultNode;
int i;
int inNodeVal = postOrder[postEnd];
for (i = inStart; i <= inEnd; i++) {
if (inNodeVal == inOrder[i])
break;
}
if (i > inStart)
resultNode -> left =
getTreeNode(inOrder, postOrder, inStart, i - 1, postStart, postStart + i - 1 - inStart);
if (i < inEnd)
resultNode -> right =
getTreeNode(inOrder, postOrder, i + 1, inEnd, postStart + i - inStart, postEnd - 1);
return resultNode;
} TreeNode* buildTree(vector<int>& inOrder, vector<int>& postOrder) {
int size = inOrder.size();
if (size == 0)
return NULL;
return getTreeNode(inOrder, postOrder, 0, size - 1, 0, size - 1);
}
};

解题描述

这道题是经典的树构建问题,通过树的中序遍历和后序遍历结果来重建树,基本的算法是通过每次从后序遍历数组末端取出元素,这个元素为当前树的根,然后再在中序遍历结果中找到这个根,根的两边分别就是左右子树。对左右子树继续递归进行相同的操作,直到数组为空即可。

[Leetcode Week14]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 -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

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

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

  9. leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal

    代码实现:给定一个中序遍历和后序遍历怎么构造出这颗树!(假定树中没有重复的数字) 因为没有规定是左小右大的树,所以我们随意画一颗数,来进行判断应该是满足题意的. 3 / \ 2 4 /\ / \1 6 ...

随机推荐

  1. tcp中的发送窗口是啥意思?

    初始的三次握手: 02:52:36.585412 IP 127.0.0.1.59764 > 127.0.0.1.8000: Flags [S], seq 3800457532, win 4369 ...

  2. [C/C++] C++模板定义格式

    函数模板的格式: template <class 形参名,class 形参名,......> 返回类型 函数名(参数列表) { //函数体 } 类模板的格式为: template<c ...

  3. [STL] 如何将一个vector赋给另一个vector

    vector 有个函数assign, 可以帮助执行赋值操作. assign会清空你的容器. assign函数: 函数原型: void assign(const_iterator first,const ...

  4. 【转】自编码算法与稀疏性(AutoEncoder and Sparsity)

    目前为止,我们已经讨论了神经网络在有监督学习中的应用.在有监督学习中,训练样本时有类别标签的.现在假设我们只有一个没带类别标签的训练样本集合  ,其中  .自编码神经网络是一种无监督学习算法,它使用了 ...

  5. 【hdu4507】吉哥系列故事——恨7不成妻 数位dp

    题目描述 求 $[L,R]$ 内满足:数位中不包含7.数位之和不是7的倍数.本身不是7的倍数 的所有数的平方和 mod $10^9+7$ . 输入 输入数据的第一行是case数T(1 <= T ...

  6. 【bzoj1076】[SCOI2008]奖励关 期望dp+状态压缩dp

    题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再 ...

  7. Python十六进制转码问题

    使用Python的decode函数转码十六进制的字符串时,会出现UnicodeDecodeError: 'utf8' codec can't decode byte 0xba in position ...

  8. lighttpd - 配置文件

    转载其他网站,收藏查看! 配置文件lighttpd.conf参数详细说明的链接和选译 发表于 2010年12月22日 http://redmine.lighttpd.net/projects/ligh ...

  9. bzoj3224: Tyvj 1728 普通平衡树(打个splay暖暖手)

    (其实今天好热啊? 题目大意:插入,删除,k小,前驱后继,数的排名. splay和treap裸题...过几天补个treap的 splay: #include<iostream> #incl ...

  10. JavaScript定义类与对象的一些方法

    最近偶然碰到有朋友问我"hoisting"的问题.即在js里所有变量的声明都是置顶的,而赋值则是在之后发生的.可以看看这个例子: 1 var a = 'global'; 2 (fu ...