博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
919. Complete Binary Tree Inserter(python+cpp)
阅读量:3701 次
发布时间:2019-05-21

本文共 3560 字,大约阅读时间需要 11 分钟。

题目:

A 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 treeremains 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:

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

解释:

完全二叉树,需要用层序遍历做。
python代码:

# Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass CBTInserter(object):    def __init__(self, root):        """        :type root: TreeNode        """        self.root=root        #层序遍历找到父节点        self.queue=[]        self.queue.append(root)        while True:            front=self.queue[0]            if not front.left:                break            self.queue.append(front.left)            if not front.right:                break            self.queue.append(front.right)            self.queue.pop(0)            def insert(self, v):        """        :type v: int        :rtype: int        """        tmpnode=TreeNode(v)        parent=self.queue[0]        if not parent.left:            parent.left=tmpnode        else:            parent.right=tmpnode            self.queue.pop(0)        self.queue.append(tmpnode)        return parent.val            def get_root(self):        """        :rtype: TreeNode        """        return self.root        # Your CBTInserter object will be instantiated and called as such:# obj = CBTInserter(root)# param_1 = obj.insert(v)# param_2 = obj.get_root()

c++代码:

/** * 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* global_root; queue
_queue; CBTInserter(TreeNode* root) {
global_root=root; _queue.push(root); while(true) {
TreeNode* tmp=_queue.front(); if(! tmp->left) break; _queue.push(tmp->left); if(!tmp->right) break; _queue.push(tmp->right); _queue.pop(); } } int insert(int v) {
TreeNode* parent=_queue.front(); TreeNode* tmpNode=new TreeNode(v); if(!parent->left) parent->left=tmpNode; else {
parent->right=tmpNode; _queue.pop(); } _queue.push(tmpNode); return parent->val; } TreeNode* get_root() {
return global_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(); */

总结:

转载地址:http://jrlcn.baihongyu.com/

你可能感兴趣的文章
鬼吹灯之龙岭迷窟
查看>>
坚果pro2刷MIUI10
查看>>
坚果pro2救砖(文末包含900E的解决方法)
查看>>
setTimeout和setInterval执行时间问题
查看>>
mouse冒泡事件
查看>>
forEach、some和every关于提前退出
查看>>
改变input (radio,check)的样式
查看>>
vue单选、多选切换,v-model字符串、数组切换
查看>>
滑动鼠标,固定导航栏一直闪烁
查看>>
奇怪!为什么@media起作用,而@media screen and不起作用
查看>>
顶部固定导航栏随着横向滚动条的滚动一起运动
查看>>
jquery获得滚动距离的方法
查看>>
js获取当前点击的元素
查看>>
为什么js代码放在head中报错,放在body中却能正常调用
查看>>
jquery中text()、html()、val()的区别
查看>>
swiper导致图片文字变模糊
查看>>
当图片被压缩时,图片变得模糊
查看>>
js外部访问局部变量
查看>>
两栏布局的几种方式
查看>>
css实现旋转、放大
查看>>