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 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.

主要涉及完全二叉树的插入。

解法一:dfs based

对给定的完全二叉树,先计算其最大高度maxlayer,以及深度最深的节点数numoflastlayer。如果numoflastlayer<2^(maxlayer-1),说明应该在maxlayer-1层第一个子节点数小于2的节点插入;如果numoflastlayer==2^(maxlayer-1),说明应该在maxlayer层第一个子节点处插入,利用dfs可以完成。插入过后及时更新maxlager和numoflastlayer。

class CBTInserter{
public:
    void preorder(TreeNode* root,int layer){
        if(layer>maxlayer){
            maxlayer = layer;
            numoflastlayer = 1;
        }else if(layer == maxlayer)
            numoflastlayer++;
        if(root->left!=NULL)
            preorder(root->left, layer+1);
        if(root->right!=NULL)
            preorder(root->right, layer+1);
    }
    CBTInserter(TreeNode* root){
        root_ =root;
        maxlayer=-1;
        numoflastlayer=0;
        preorder(root,0);
    }
    TreeNode* Insert(TreeNode* root, int v, int layer, int insertlayer){
        if(layer == insertlayer-1){
            if(root->left == NULL){
                root->left = new TreeNode(v);
                return root;
            }else if(root->right == NULL){
                root->right = new TreeNode(v);
                return root;
            }
        }else{
            TreeNode* res = Insert(root->left, v, layer+1, insertlayer);
            if(res == NULL)
                res = Insert(root->right, v, layer+1, insertlayer);
            return res;
        }
        return NULL;
    }
    int insert(int v){int maxnumoflastlayer = pow(2, maxlayer);
        TreeNode* res = NULL;
        if(numoflastlayer<maxnumoflastlayer){
            res = Insert(root_,v,0, maxlayer);
            numoflastlayer++;
        }else{
            res = Insert(root_,v,0,maxlayer+1);
            maxlayer++;
            numoflastlayer=1;
        }
        return res->val;
    }
    TreeNode* get_root(){
        return root_;
    }
private:
    TreeNode* root_;
    int maxlayer;
    int numoflastlayer;
};

解法二:bfs based

先使用bfs将所有子节点数为0和1的节点存入队列,然后维护这个队列,对头节点是插入新节点的节点,若对头节点只有右子树为NULL,那么插入后将其pop,并将其两个子节点指针压入队列。

class CBTInserter{
public:
    TreeNode* root_;
    queue<TreeNode*> nodes_0_1;
    CBTInserter(TreeNode* root){
        root_ = root;
        queue<TreeNode*> que;
        que.push(root);
        while(!que.empty()){
            TreeNode* now = que.front();
            que.pop();
            if(now->left == NULL)
                nodes_0_1.push(now);
            else if(now->right == NULL)
                nodes_0_1.push(now);
            else{
                que.push(now->left);
                que.push(now->right);
            }
        }
    }
    int insert(int v){
        TreeNode* root = nodes_0_1.front();
        if(root->left!=NULL){
            root->right = new TreeNode(v);
            nodes_0_1.pop();
            nodes_0_1.push(root->left);
            nodes_0_1.push(root->right);
        }
        else
            root->left = new TreeNode(v);
        return root->val;
    }
    TreeNode* get_root(){
        return root_;
    }
};

原文地址:https://www.cnblogs.com/jasonlixuetao/p/10583374.html

时间: 2024-10-09 20:10:24

leetcode_919. Complete Binary Tree Inserter的相关文章

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

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

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

PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)

题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则即不是. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> using namespace std; const int maxn=22; int two

[CF792D] Paths in a Complete Binary Tree (规律, 位运算, lowbit)

题目链接:http://codeforces.com/problemset/problem/792/D 画出树,找找规律,画图就好了.不算麻烦. 往下走的时候特判是不是叶子,往上走的时候特判是不是根.其余时候按照规律转移就是. 感觉可以推广到建树上,可以缩小常数是极好的. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 LL lowbit(LL x) { return x & (

PAT (Advanced Level) 1110. Complete Binary Tree (25)

判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n,root; const int maxn=30; struc

【PAT甲级】1110 Complete Binary Tree (25分)

题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点. trick: 用char输入子结点没有考虑两位数的结点??... stoi(x)可以将x转化为十进制整数 AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace