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 duplicates do not exist in the tree.
========
利用:中序+后序遍历
====
code:
/**
* 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:
///
template<typename Iterator>
TreeNode *help_buildTree_ip(Iterator in_first,Iterator in_last,
Iterator post_first,Iterator post_last){
if(in_first == in_last) return nullptr;
if(post_first == post_last) return nullptr; auto val = *prev(post_last);
TreeNode *root = new TreeNode(val); auto in_root_pos = find(in_first,in_last,val);
auto left_size = distance(in_first,in_root_pos);
auto post_left_last = next(post_first,left_size); root->left = help_buildTree_ip(in_first,in_root_pos,post_first,post_left_last);
root->right = help_buildTree_ip(next(in_root_pos),in_last,post_left_last,prev(post_last)); return root;
}
TreeNode* buildTree_ip(vector<int> &inorder,vector<int> &postorder){
return help_buildTree_ip(inorder.begin(),inorder.end(),postorder.begin(),postorder.end());
}
};
106. Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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 OJ 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】105 & 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 ...
- 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 由中序和后序遍历建立二叉树 C++
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- 运动历史图(MHI)——Motion History Image
MHI最初是由Bobick 和 Davis提出的,在此之前,Bobick 和 Davis 首先提出了二值的运动能量图(Motion Energy Image,MEI),通过描述物体如何移动和运动在空间 ...
- 382. Linked List Random Node
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- Codeforces Round #372 (Div. 2) A B C 水 暴力/模拟 构造
A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- ajax 城市区域选择三级联动
<body onLoad="sheng()"><div class="xqbody"> <form action=" ...
- tyvj1011 - 传纸条 ——DP
题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1011 状态转移方程: f[k,x1,x2] = max(f[k-1,x1,x2],f[k-1,x1-1, ...
- leetcode 119 Pascal's Triangle II ----- java
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- svn 安装 、使用(1)
写在开头: 虽然网络极大的方便了我们查找答案,而且有很多人写各样的博客.但每个人在实际中的情况不一样,遇到的问题也不一样,大牛们会把步骤写的很简单,可能真的是怕麻烦,但显然就有一些东西已经不适合一部分 ...
- Debugging a Parallel Application
Walkthrough: Debugging a Parallel Application https://msdn.microsoft.com/en-us/library/dd554943.aspx ...
- ADS1110/ADS1271
ADS1110 1.初始化 软件:设置p任意2个为GPIO口 硬件:设置p0.2,p0.3为SDA,SCL 输入.输出 ADS1110的I2C地址(1001aaa)例如ADS1110A0的地址是100 ...
- HTTPS-HSTS协议(强制客户端使用HTTPS与服务器创建连接)
HSTS(HTTP Strict Transport Security)国际互联网工程组织IETE正在推行一种新的Web安全协议 HSTS的作用是强制客户端(如浏览器)使用HTTPS与服务器创建连接. ...