• 【leetcode】往完全二叉树添加节点


    一、题目描述

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

    设计一个用完全二叉树初始化的数据结构 CBTInserter,它支持以下几种操作:

    • CBTInserter(TreeNode root) 使用根节点为 root 的给定树初始化该数据结构;
    • CBTInserter.insert(int v) 向树中插入一个新节点,节点类型为 TreeNode,值为 v 。使树保持完全二叉树的状态,并返回插入的新节点的父节点的值;
    • CBTInserter.get_root() 将返回树的根节点。
    二、代码思路
    • 首先,得读懂题目:
      • 1、 题目首先给我们传来了一个root节点,已经实现了完全二叉树。
      • 2、 其次,我们只需要做两件事即可,其一:就是遍历这个二叉树并找到适当的位置,将节点插入即可。其二:经过多次插入之后,我们返回新的完全二叉树。
      • 3、 总的来说,我们其实主要做的就是第二步,遍历二叉树,找到需要插入的节点,并将节点插入即可,至于返回新的二叉树,我们直接还是返回root就行,因为我们本身就是在它身上进行的插入操纵。
    • 其次,我们就主要完成第二部步,也就是完全二叉树的遍历和插入。
      • 1、我们遍历树可以使用递归和非递归,但是对于完全二叉树的遍历我们使用层序遍历即可,需要维护一个队列即可。
      • 2、 何时插入元素呢?其实很简单,如果我们按照层序遍历的顺序,如果发现有一个节点的左或者右没有节点,就是我们需要插入的位置。
    三、代码题解
    /**
     * 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 {
    
        private TreeNode root;    
        public CBTInserter(TreeNode root) {
            this.root = root;
        }
        //其实完全二叉树的遍历就是层序遍历
        //使用的数据结构就是队列
        public int insert(int v) {
            TreeNode node = root;
            Queue<TreeNode> queue = new LinkedList();
            queue.add(root);
            while (!queue.isEmpty()) {
                node = queue.poll();
                if (node.left != null) {
                    queue.add(node.left);
                } else {
                    node.left = new TreeNode(v);
                    return node.val;
                }
                if (node.right != null) {
                    queue.add(node.right); 
                } else {
                    node.right = new TreeNode(v);
                    return node.val;
                }
            }
            return -1;
        }
        
        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(v);
     * TreeNode param_2 = obj.get_root();
     */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
  • 相关阅读:
    WebGL☀️Unity WebGL适配到各平台的教程
    Linux:管道命令与文本处理三剑客(grep、sed、awk)
    编辑器插件
    Win10无法连接打印机怎么办?不能使用打印机的解决方法
    论文翻译1-----DSSM:Deep Structured Semantic Models
    用FIR滤波器设计数字微分器和数字希尔伯特变换器
    聊聊什么是SpringBoot 的自动装配原理
    Eastern Exhibition【中位数 距离和的最小值】
    [CTF]2022美团CTF WEB WP
    Docker Daemon
  • 原文地址:https://blog.csdn.net/weixin_44627672/article/details/126814384