889. 根据前序和后序遍历构造二叉树(非递归)

题目连接:

https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/

题目大意:

中文题目

AC代码:

 1 TreeNode * construcuFormPrePost(vrctor<int>& pre, vector<int>&post){
 2
 3     if(pre.empty())
 4         return NULL;
 5     stack<TreeNode*>S;
 6     auto root = new Treenode(pre[0]);
 7     S.push(root);
 8     for(int i = 1, j = 0; i <pre.size(); i++){
 9         auto node = new TreeNode(pre[i]);
10         while(S.top()->val == post[j])S.pop(), j++;
11         if(!S.top()->left)
12             S.top()->left = node;
13         else
14             S.top()->right = node;
15         S.push(node);
16     }
17     return root;
18 }

原文地址:https://www.cnblogs.com/letlifestop/p/11517160.html

时间: 2024-10-25 18:45:20

889. 根据前序和后序遍历构造二叉树(非递归)的相关文章

中序和后序遍历构造二叉树

题目链接: 涉及知识:二叉树的遍历 分析: 上一篇中介绍了如何通过二叉树的前序和中序遍历构造二叉树. 我们知道前序的遍历顺序是:根,左,右:中序的遍历顺序是左,根,右:后序的遍历顺序是左,右,根: 如果我们将后序遍历倒过来看便是根,右,左:会发现和前序遍历是非常相似的.前序遍历依次是根节点,左子树根节点,右子树根节点:后序遍历倒过来依次是根节点,右子树根节点,左子树根节点:因此解法和前序+中序生成后序的思路一样,从后序遍历中倒过来查找根节点,且先生成该节点的右子树,在生成该节点的左子树即可. 代

[二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)

1119. Pre- and Post-order Traversals (30) Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversa

leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

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 duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

二叉树(15)----由中序遍历和后序遍历重建二叉树,递归方式

1.二叉树定义 typedef struct BTreeNodeElement_t_ { void *data; } BTreeNodeElement_t; typedef struct BTreeNode_t_ { BTreeNodeElement_t *m_pElemt; struct BTreeNode_t_ *m_pLeft; struct BTreeNode_t_ *m_pRight; } BTreeNode_t; 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 duplicates do not exist in the tree. 思路:这题和上题类似,前序第一个是根节点,后序遍历最后一个是根节点.其余步骤类似. 代码如下: /** * Definition for a binary tree node. * public class TreeNod

笔试算法题(36):寻找一棵二叉树中最远节点的距离 &amp; 根据二叉树的前序和后序遍历重建二叉树

出题:求二叉树中距离最远的两个节点之间的距离,此处的距离定义为节点之间相隔的边数: 分析: 最远距离maxDis可能并不经过树的root节点,而树中的每一个节点都可能成为最远距离经过的子树的根节点:所以计算出以每个节点为根节点的子树的最 远距离,最后取他们的最大值就是整棵树的最远距离: 如果递归层次过多造成系统栈溢出,则可以使用stack堆栈结构存储递归节点,从而使用循环实现 解题: 1 struct Node { 2 int value; 3 Node *left; 4 Node *right

leetcode-105-从前序与中序遍历构造二叉树

题目描述: 方法一: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode: def helper(in_l

leetcode-106-从中序和后序遍历构造二叉树

题目描述: 方法一:O(n) O(n) class Solution: def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode: assert len(inorder)==len(postorder) if len(inorder)==0: return None if len(inorder)==1: return TreeNode(inorder[0]) pos = inorder.index(pos