龙空技术网

「教3妹学算法-每日3题(1)」完全二叉树插入器

攻城狮大兵 229

前言:

而今大家对“算法分析之二叉树插入删除”大致比较关切,同学们都需要剖析一些“算法分析之二叉树插入删除”的相关知识。那么小编也在网络上搜集了一些关于“算法分析之二叉树插入删除””的相关资讯,希望各位老铁们能喜欢,看官们快快来了解一下吧!

3妹

3妹:早啊, 2哥

2哥:3妹今天怎么这么开心

3妹:因为今天是周五啊, 每个周五我都会很开心, 因为明后天就不用上班了呀。

2哥:晚上去看电影怎么样。

3妹:可以的, 看喜剧片吗?

2哥:我无所谓, 哪怕不看喜剧片,想到明天不用上班,也能看出喜剧片的效果,哈哈。

3妹:ok, 晚上见, 我要去上班啦。

2哥:别忘记通勤路上看看算法题,不能偷懒哈。

讲课

题目:

完全二叉树 是每一层(除最后一层外)都是完全填充(即,节点数达到最大)的,并且所有的节点都尽可能地集中在左侧。

设计一种算法,将一个新节点插入到一个完整的二叉树中,并在插入后保持其完整。

实现 CBTInserter 类:

CBTInserter(TreeNode root) 使用头节点为 root 的给定树初始化该数据结构;

CBTInserter.insert(int v) 向树中插入一个值为 Node.val == val的新节点 TreeNode。使树保持完全二叉树的状态,并返回插入节点 TreeNode 的父节点的值;

CBTInserter.get_root() 将返回树的头节点。

示例 1:

输入

["CBTInserter", "insert", "insert", "get_root"]

[[[1, 2]], [3], [4], []]

输出

[null, 1, 2, [1, 2, 3, 4]]

解释

CBTInserter cBTInserter = new CBTInserter([1, 2]);

cBTInserter.insert(3); // 返回 1

cBTInserter.insert(4); // 返回 2

cBTInserter.get_root(); // 返回 [1, 2, 3, 4]

提示:

树中节点数量范围为 [1, 1000]

0 <= Node.val <= 5000

root 是完全二叉树

0 <= val <= 5000

每个测试用例最多调用 insert 和 get_root 操作 10^4 次

思路:

对于一棵完全二叉树而言,其除了最后一层之外都是完全填充的,并且最后一层的节点全部在最左侧。那么,只有倒数第二层(如果存在)最右侧的若干个节点,以及最后一层的全部节点可以再添加子节点,其余的节点都已经拥有两个子节点。

因此,我们可以使用一个队列存储上述提到的这些可以添加子节点的节点。队列中的存储顺序为:首先「从左往右」存储倒数第二层最右侧的节点,再「从左往右」存储最后一层的全部节点。这一步可以使用广度优先搜索来完成,因为广度优先搜索就是按照层优先进行遍历的。

随后,当我们每次调用insert(val) 时,我们就创建出一个节点 child,并将它最为队列的队首节点的子节点。在这之后,我们需要把 child 加入队尾,并且如果对队首节点已经有两个子节点,我们需要将其从队列中移除。

java代码:

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode() {} *     TreeNode(int val) { this.val = val; } *     TreeNode(int val, TreeNode left, TreeNode right) { *         this.val = val; *         this.left = left; *         this.right = right; *     } * } */ class CBTInserter {    Queue<TreeNode> candidate;    TreeNode root;    public CBTInserter(TreeNode root) {        this.candidate = new ArrayDeque<TreeNode>();        this.root = root;        Queue<TreeNode> queue = new ArrayDeque<TreeNode>();        queue.offer(root);        while (!queue.isEmpty()) {            TreeNode node = queue.poll();            if (node.left != null) {                queue.offer(node.left);            }            if (node.right != null) {                queue.offer(node.right);            }            if (!(node.left != null && node.right != null)) {                candidate.offer(node);            }        }    }    public int insert(int val) {        TreeNode child = new TreeNode(val);        TreeNode node = candidate.peek();        int ret = node.val;        if (node.left == null) {            node.left = child;        } else {            node.right = child;            candidate.poll();        }        candidate.offer(child);        return ret;    }    public 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(val); * TreeNode param_2 = obj.get_root(); */

标签: #算法分析之二叉树插入删除