LeetCode周赛#105 Q3 Complete Binary Tree Inserter (二叉树)

题目来源:https://leetcode.com/contest/weekly-contest-105/problems/complete-binary-tree-inserter/

问题描述

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.

Write a data structure CBTInserter that is initialized with a complete binary tree and supports the following operations:

  • 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 tree with value node.val = v so that the tree remains complete, and returns the value of the parent of the inserted TreeNode;
  • CBTInserter.get_root() will return the head node of the tree.

 

Example 1:

Input: inputs = ["CBTInserter","insert","get_root"], inputs = [[[1]],[2],[]]
Output: [null,1,[1,2]]

Example 2:

Input: inputs = ["CBTInserter","insert","insert","get_root"], inputs = [[[1,2,3,4,5,6]],[7],[8],[]]
Output: [null,3,4,[1,2,3,4,5,6,7,8]]

 

Note:

  1. The initial given tree is complete and contains between 1 and 1000 nodes.
  2. CBTInserter.insert is called at most 10000 times per test case.
  3. Every value of a given or inserted node is between 0 and 5000.

------------------------------------------------------------

题意

给定二叉树节点类,要求写出完全二叉树的节点插入器类的构造函数和插入函数。所谓“完全二叉树”是指节点尽量靠左的二叉树。

------------------------------------------------------------

思路

想清楚类属性要有两个:一个是根节点的指针Root,一个是树的大小cnt(节点总数)。构造函数计算出cnt,插入函数用BFS中序遍历树找到最左的空节点插入。

------------------------------------------------------------

代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class CBTInserter {
public:
    TreeNode* Root;
    int cnt;
    
    CBTInserter(TreeNode* root) {
        Root = root;
        cnt = 0;
        // bfs calculating CBT nodes number
        queue<TreeNode*> q;
        TreeNode *p;
        q.push(Root);
        while (!q.empty())
        {
            p = q.front();
            q.pop();
            cnt++;
            if (p->left)
            {
                q.push(p->left);
            }
            if (p->right)
            {
                q.push(p->right);
            }
        }
    }
    
    int insert(int v) {
        TreeNode *p = Root;
        vector<bool> vec;
        int vv = (cnt+1)/2;
        while (vv > 0)
        {
            vec.push_back(vv&1);
            vv = vv >> 1;
        }
        int vlen = vec.size(), i;
        for (i=vlen-2; i>=0; i--)
        {
            if (vec[i])
            {
                p = p->right;
            }
            else
            {
                p = p->left;
            }
        }
        if (cnt&1 == 1)
        {
            p->left = new TreeNode(v);
        }
        else
        {
            p->right = new TreeNode(v);
        }
        cnt++;
        return p->val;
    }
    
    TreeNode* get_root() {
        return Root;
    }
};

/**
 * Your CBTInserter object will be instantiated and called as such:
 * CBTInserter obj = new CBTInserter(root);
 * int param_1 = obj.insert(v);
 * TreeNode* param_2 = obj.get_root();
 */

猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/83007155
今日推荐