[Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解
原创文章,拒绝转载
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的更多相关文章
- 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: ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- (二叉树 递归) 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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 【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 ...
- leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal
代码实现:给定一个中序遍历和后序遍历怎么构造出这颗树!(假定树中没有重复的数字) 因为没有规定是左小右大的树,所以我们随意画一颗数,来进行判断应该是满足题意的. 3 / \ 2 4 /\ / \1 6 ...
随机推荐
- 【Python】python基础语法 编码
编码 默认情况下,python以UTF-8编码,所有的字符串都是Unicode字符串,可以为代码定义不同的的编码. #coding:UTF-8 #OR #-*- coding:UTF-8 -*- p ...
- 只有父类的init方法才有创建servletConfig与servletContext的能力
如果重写了父类的init方法 但又没有显示调用父类的init方法 那么子类无法使用servletContext 因为 子类方法没有创建出 servletConfig
- [洛谷P5173]传球
题目大意:有$n(n\leqslant3500)$个人坐成一个环,$0$号手上有个球,每秒钟可以向左或向右传球,问$m$秒后球在$0$号手上的方案数. 题解:一个$O(nm)$的$DP$,$f_{i, ...
- [NOI2009]诗人小G 决策单调性优化DP
第一次写这种二分来优化决策单调性的问题.... 调了好久,,,各种细节问题 显然有DP方程: $f[i]=min(f[j] + qpow(abs(sum[i] - sum[j] - L - 1))); ...
- Jquery常用正则验证
常用校验的正则表达式var rulesConfig = { /** * str.replace(/^\s+|\s+$/g, '') 解析: str:要替换的字符串 \s : 表示 space ,空格 ...
- POJ1006:Biorhythms——题解
http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...
- Java CPU占用率高分析
首先,通过top命令找出CPU占用率高的进程: 然后,通过ps -o THREAD,tid,time -mp 2066命令找出执行时间最长的线程的TID 将有问题的TID转为16进制格式: print ...
- snmp理论篇
SNMP协议入门 1.引言 基于TCP/IP的网络管理包含3个组成部分: 1) 一个管理信息库MIB(Management Information Base).管理信息库包含所有代理进程的所有可被查询 ...
- is(':checked'); 判断radio是否选中,该属性会随着radio被切换点击而变化
// var is_rec =$("#is_rec_on").is(':checked'); //判断radio是否选中,该属性会随着radio被切换点击而变化 // if(is_ ...
- (转)如何在windows 2008 安装IIS
首先声明本文转自http://www.pc6.com/infoview/Article_54712.html ,作者为清晨 转载的原因有两个,一是怕原文挂了,而是打算写一下在阿里云部署django的文 ...