leetcode中二叉树的类定义解释

2023-12-23

希望有人能帮助我了解这门课是如何运作的。 我目前正在 udemy 中学习 JavaScript 算法,它们解释如何在二叉树中执行所有操作的方式与 leetcode 显示的稍有不同。

课程中,树的定义与leetcode相同或非常相似:

class Node {
    constructor(value){
        this.value = value;
        this.left = null;
        this.right = null;
    }
} 

class BinarySearchTree {
    constructor(){
        this.root = null;
    }
}

但是,在执行任何其他操作之前,这些值首先作为节点插入:

insert(value){
        var newNode = new Node(value);
        if(this.root === null){
            this.root = newNode;
            return this;
        }
        var current = this.root;
        while(true){
            if(value === current.value) return undefined;
            if(value < current.value){
                if(current.left === null){
                    current.left = newNode;
                    return this;
                }
                current = current.left;
            } else {
                if(current.right === null){
                    current.right = newNode;
                    return this;
                } 
                current = current.right;
            }
        }
    }

在 Leetcode 上,值作为数组传递,这让我有点困惑:

二叉树节点的定义。

* function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }

* @param {TreeNode} root
 * @return {number}

查看寻找最大深度的简单解决方案:

var maxDepth = function(root) {
     if(!root) return 0;
    
    return Math.max(maxDepth(root.left) , maxDepth(root.right) ) +1
};

给定数组 root = [3,9,20,null,null,15,7],

我们怎么知道 root.left 是 9,root.right 是 20。那么下一级,root.left.left 为 null,root.left.right 为 null。那么 root.right.left 是 15,root.right.right 是 7。

只是不确定数组如何转换成那个

Thanks!

尝试将节点一一添加,然后执行二叉树操作


在 Leetcode 上,值作为数组传递

这是一个误解。

值不作为数组传递。你的函数得到一个实例TreeNode作为参数(或null)。 LeetCode 让你specify以某种 JSON 格式输入,但这只是 LeetCode 首先将其转换为的文本TreeNode基于树,在调用您的函数之前。

文本输入遵循对其所代表的树的一种面包优先遍历。所以[3,9,20,null,null,15,7]代表这棵树:

               3
              / \
             9   20
                /  \
              15    7

The two null出现次数表明值为 9 的节点没有左子节点或右子节点。

如果您想自己将数组(基于输入符号)转换为树(LeetCode 为您完成的任务),您可以使用如下函数:

function TreeNode(val, left, right) { // LeetCode's code
    this.val = (val===undefined ? 0 : val)
    this.left = (left===undefined ? null : left)
    this.right = (right===undefined ? null : right)
}

function makeTree(arr) {
    if (!arr) return null; // empty tree
    const values = arr[Symbol.iterator]();
    const root = new TreeNode(values.next().value);
    const queue = new Set().add(root);
    for (const node of queue) {
        for (const side of ["left", "right"]) {
             const value = values.next().value;
             if (value != null) queue.add(node[side] = new TreeNode(value));
        }
    }
    return root;
}

// Example use
const arr = [3,9,20,null,null,15,7];
const root = makeTree(arr);
console.log(root);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

leetcode中二叉树的类定义解释 的相关文章