• 919. Complete Binary Tree Inserter


    complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

    Design an algorithm to insert a new node to a complete binary tree keeping it complete after the insertion.

    Implement the CBTInserter class:

    • CBTInserter(TreeNode root) Initializes the data structure with the root of the complete binary tree.
    • int insert(int v) Inserts a TreeNode into the tree with value Node.val == val so that the tree remains complete, and returns the value of the parent of the inserted TreeNode.
    • TreeNode get_root() Returns the root node of the tree.

    Example 1:

    Input
    ["CBTInserter", "insert", "insert", "get_root"]
    [[[1, 2]], [3], [4], []]
    Output
    [null, 1, 2, [1, 2, 3, 4]]
    
    Explanation
    CBTInserter cBTInserter = new CBTInserter([1, 2]);
    cBTInserter.insert(3);  // return 1
    cBTInserter.insert(4);  // return 2
    cBTInserter.get_root(); // return [1, 2, 3, 4]
    

    Constraints:

    • The number of nodes in the tree will be in the range [1, 1000].
    • 0 <= Node.val <= 5000
    • root is a complete binary tree.
    • 0 <= val <= 5000
    • At most 104 calls will be made to insert and get_root.

    题目:实现一个类,可以往二叉树中插入节点,使得二叉树保持完全二叉树

    思路:容易错的点是初始化时不只是一个根节点,可能已经是一棵树。用queue来保存所有不完全的子节点。代码

     

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */
    12. class CBTInserter {
    13. private:
    14. queue q;
    15. TreeNode* rootNode;
    16. public:
    17. CBTInserter(TreeNode* root) {
    18. rootNode = root;
    19. q.push(root);
    20. while(!q.empty()){
    21. if(q.front()->left) q.push(q.front()->left);
    22. if(q.front()->right) q.push(q.front()->right);
    23. if(q.front()->left && q.front()->right)
    24. q.pop();
    25. else break;
    26. }
    27. }
    28. int insert(int val) {
    29. TreeNode* node = new TreeNode(val);
    30. int res = q.front()->val;
    31. if(!q.front()->left){
    32. q.front()->left = node;
    33. } else {
    34. q.front()->right = node;
    35. q.pop();
    36. }
    37. q.push(node);
    38. return res;
    39. }
    40. TreeNode* get_root() {
    41. return rootNode;
    42. }
    43. };
    44. /**
    45. * Your CBTInserter object will be instantiated and called as such:
    46. * CBTInserter* obj = new CBTInserter(root);
    47. * int param_1 = obj->insert(val);
    48. * TreeNode* param_2 = obj->get_root();
    49. */

  • 相关阅读:
    在易语言中调用MS SQL SERVER数据库存储过程方法总结
    Mysql-SQL优化
    《Effective C++》阅读总结(三):资源管理
    RabbitMQ的5大核心概念
    YOLOv7改进策略:一种新颖的可扩张残差(DWR)注意力模块,增强多尺度感受野特征,助力小目标检测
    RocksDB基本架构与原理详解
    【算法刷题day34】Leetcode:1005. K 次取反后最大化的数组和、134. 加油站、135. 分发糖果
    minio单点及分布式部署
    【Git】学习笔记
    MongoDB - 增删改查
  • 原文地址:https://blog.csdn.net/qing2019/article/details/126383866