关于后端:919-完全二叉树插入器-简单-BFS-运用题

2次阅读

共计 2361 个字符,预计需要花费 6 分钟才能阅读完成。

题目形容

这是 LeetCode 上的 919. 齐全二叉树插入器 ,难度为 中等

Tag :「模仿」、「BFS」、「树的遍历」

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

设计一种算法,将一个新节点插入到一个残缺的二叉树中,并在插入后放弃其残缺。

实现 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$ 次

BFS

起始应用数组对构造函数传入的 root 进行 BFS 层序遍历(因为仍要保留层序遍历程序,因而应用下标指针 cur 来模拟出队地位)。

对于 insert 操作而言,咱们要在数组(层序遍历程序)中找到首个「存在左右空子节点」的父节点 fa,因为找到 fa 节点的过程数组下标枯燥递增,因而能够应用全局变量 idx 一直往后搜寻,每次将新节点 node 增加到以后树后,须要将 node 增加到数组尾部。

get_root 操作则始终返回数组首位元素即可。

Java 代码:

class CBTInserter {List<TreeNode> list = new ArrayList<>();
    int idx = 0;
    public CBTInserter(TreeNode root) {list.add(root);
        int cur = 0;
        while (cur < list.size()) {TreeNode node = list.get(cur);
            if (node.left != null) list.add(node.left);
            if (node.right != null) list.add(node.right);
            cur++;
        }
    }
    public int insert(int val) {TreeNode node = new TreeNode(val);
        while (list.get(idx).left != null && list.get(idx).right != null) idx++;
        TreeNode fa = list.get(idx);
        if (fa.left == null) fa.left = node;
        else if (fa.right == null) fa.right = node;
        list.add(node);
        return fa.val;
    }
    public TreeNode get_root() {return list.get(0);
    }
}

TypeScript 代码:

class CBTInserter {list: TreeNode[] = new Array<TreeNode>()
    idx: number = 0
    constructor(root: TreeNode | null) {this.list.push(root)
        let cur = 0
        while (cur < this.list.length) {const node = this.list[cur]
            if (node.left != null) this.list.push(node.left)
            if (node.right != null) this.list.push(node.right)
            cur++
        }
    }
    insert(val: number): number {const node = new TreeNode(val)
        while (this.list[this.idx].left != null && this.list[this.idx].right != null) this.idx++
        const fa = this.list[this.idx]
        if (fa.left == null) fa.left = node
        else if (fa.right == null) fa.right = node
        this.list.push(node)
        return fa.val
    }
    get_root(): TreeNode | null {return this.list[0]
    }
}
  • 工夫复杂度:$O(n)$
  • 空间复杂度:$O(n)$

最初

这是咱们「刷穿 LeetCode」系列文章的第 No.919 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,局部是有锁题,咱们将先把所有不带锁的题目刷完。

在这个系列文章外面,除了解说解题思路以外,还会尽可能给出最为简洁的代码。如果波及通解还会相应的代码模板。

为了不便各位同学可能电脑上进行调试和提交代码,我建设了相干的仓库:https://github.com/SharingSou…。

在仓库地址里,你能够看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其余优选题解。

更多更全更热门的「口试 / 面试」相干材料可拜访排版精美的 合集新基地 🎉🎉

本文由 mdnice 多平台公布

正文完
 0