• 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. */

  • 相关阅读:
    Android Compose Bloom 项目实战 (二) : 欢迎页
    【luogu CF487E】Tourists(圆方树)(树链剖分)(线段树)
    mvn deploy tongweb-embed-7.0.E.5_P3 依赖上传
    CatchAdmin实战教程(四)Table组件之自定义基础页面
    爬取某牙视频
    无线WiFi安全渗透与攻防(五) aircrack-ng(亲测有效)、mdk3联合攻击
    数据结构--基数排序(考察不多,会手动模拟即可)
    [2023年]-hadoop面试真题(二)
    pyspark.sql.types.MapType()的使用
    Feign 和 OpenFeign 的区别???
  • 原文地址:https://blog.csdn.net/qing2019/article/details/126383866