Leetcode-919 Complete Binary Tree Inserter(完全二叉树插入器)

 1 vector<TreeNode> ve(16385,0);
 2 class CBTInserter
 3 {
 4     public:
 5         queue<TreeNode*> q;
 6         int veEnd;
 7
 8         CBTInserter(TreeNode* root)
 9         {
10             TreeNode rubbish(0);
11             veEnd = 0;
12             ve[veEnd++] = (rubbish);
13             q.push(root);
14             Init();
15             for(int i = 2; i < veEnd; i ++)
16             {
17                 if((i&0x1)==1)
18                 {
19                     ve[i/2].right = &ve[i];
20                 }
21                 else
22                 {
23                     ve[i/2].left = &ve[i];
24                 }
25             }
26         //    cout << "??" << ve.size() << endl;
27         }
28
29         int insert(int v)
30         {
31             TreeNode t(v);
32             ve[veEnd++] = (t);
33             if((veEnd%2)==1)
34             {
35                 ve[(veEnd-1)/2].left = &ve[veEnd-1];
36             }
37             else
38             {
39                 ve[(veEnd-1)/2].right = &ve[veEnd-1];
40             }
41             return ve[(veEnd-1)/2].val;
42         }
43
44         TreeNode* get_root()
45         {
46             return &ve[1];
47         }
48
49         void Init()
50         {
51             while(!q.empty())
52             {
53                 TreeNode t = *(q.front());
54                 ve[veEnd++] = (*(q.front()));
55                 if(q.front()->left)
56                     q.push(q.front()->left);
57                 if(q.front()->right)
58                     q.push(q.front()->right);
59                 q.pop();
60             }
61         }
62 };

哔了狗,一直忘记vector开辟空间以后会把所有元素的地址进行变动,然后疯狂debug

原文地址:https://www.cnblogs.com/Asurudo/p/9763524.html

时间: 2024-11-13 12:03:37

Leetcode-919 Complete Binary Tree Inserter(完全二叉树插入器)的相关文章

leetcode_919. Complete Binary Tree Inserter

https://leetcode.com/problems/complete-binary-tree-inserter/ 设计一个CBTInserter,使用给定完全二叉树初始化.三个功能; CBTInserter(TreeNode root) initializes the data structure on a given tree with head node root; CBTInserter.insert(int v) will insert a TreeNode into the t

PAT甲级——1110 Complete Binary Tree (完全二叉树)

此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For eac

1110 Complete Binary Tree (25分) 判断一棵二插树是否是完全二叉树

Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤) which is the total number of nodes in the tree -- and he

PAT1110:Complete Binary Tree

1110. Complete Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For each case, th

PAT 1110 Complete Binary Tree[比较]

1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤20) which is the total nu

LeetCode OJ - Balanced Binary Tree

判断树是否是平衡的,这道题中的平衡的概念是指任意节点的两个子树的高度相差不超过1,我用递归的方法把所有的节点的高度都计算了下,并且在计算的过程记录每个节点左右两颗子树的高度差,最后通过遍历这个高度差就可以知道是否是平衡的. 下面是AC代码: 1 /** 2 * Given a binary tree, determine if it is height-balanced. 3 * For this problem, a height-balanced binary tree is defined

LeetCode OJ - construct Binary Tree from Inorder and Postorder/Preorder Traversal

不断递归的实现!!!! 下面是AC代码: 1 /** 2 * Given inorder and postorder traversal of a tree, construct the binary tree. 3 * @param inorder 4 * @param postorder 5 * @return 6 */ 7 public TreeNode buildTree(int[] inorder,int[] postorder){ 8 if(inorder == null || po

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 -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 traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 代码: class Solution { public: TreeNode *buildTr