[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 ...
随机推荐
- 第29天:js-数组添加删除、数组和字符串相互转换
一.添加数组var arr=[1,3,5];arr.push(7,9);//添加7和9到数组arr后面,得到[1,3,5,7,9]1.push();可向数组末尾添加一个或多个元素,并返回新的长度.2. ...
- asp.net MVC4 @Html.DropDownList的使用
在MVC4中使用Razor语法,一使用就爱上他了, 一般项目都是有一些增删改查功能,表单下拉框是经常使用的,除了用原始的<select>外,还可以用@Html.DropDownList和@ ...
- bzoj4753[JSOI2016]最佳团体
题意:01分数规划,但可选的数字之间存在森林形的依赖关系(可以认为0号点是个虚根,因为并不能选). 虽然有森林形的依赖关系,但还是可以套分数规划的思路,二分答案k,判断是否存在一个比值大于k的方案 即 ...
- 【bzoj4579】[Usaco2016 Open]Closing the Farm 并查集
题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to tem ...
- BZOJ4770 图样(概率期望+动态规划)
考虑求出所有MST的权值和再除以方案数,方案数显然是2mn. 按位考虑,显然应该让MST里的边高位尽量为0.那么根据最高位是0还是1将点集划分成两部分,整张图的MST就是由两部分各自的MST之间连一条 ...
- 协程-Greenlet
协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈. 线程切换的时候会保存到CPU里面. 因此: 协程能保留上一次调用时的 ...
- [luogu3806]【模板】点分治1
description 求树上长度为\(k\)的路径是否存在. data range \[n\le 10000,k\le 10000000\] solution 点分治复习... 使用普通的点分治枚举 ...
- C++中static用法
本文为个人学习笔记,参考<C++ Primer(中文第五版)>和<王道程序员求职宝典> 本文分为两个部分:不考虑类.类中static的作用 一.不考虑类,static的作用 1 ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- BZOJ1999 NOIP2007 洛谷P1099 P2491 SDOI 2011
Description: 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边到有正整数的权,我们称T为树网(treebetwork),其中V,E分别表示结点与边的集合,W表示各 ...