简体   繁体   English

当某些节点为 null 时,二叉树搜索不返回匹配值

[英]Binary tree search not returning matching value when some nodes are null

TL;DR - How do I alter this algorithm to return the matching val given a BST with null nodes? TL;DR - 如何更改此算法以返回给定具有 null 节点的 BST 的匹配 val?

TreeNode:树节点:

/**
 * Definition for a binary tree node.
 * 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
 * @param {number} val
 * @return {TreeNode}
 */

Given a binary tree, find the matching val.给定一棵二叉树,找到匹配的 val。 My algorithm is below我的算法如下

var searchBST = function(root, val) {
    const matchingNode = visitNode(root, val);
    // if no match, return null
    return matchingNode ? matchingNode : null;
};

const visitNode = (node, val) => {
    if (node.left !== null) {
        return visitNode(node.left, val)
    }
    if (node.right !== null) {
        return visitNode(node.right, val)
    }
    if (node.val === val) {
        console.log('MATCH!')
        return node;
    }
}

This works for standard trees with no null values, such as [4,2,7,1,3] and empty trees [].这适用于没有null值的标准树,例如 [4,2,7,1,3] 和空树 []。

I'm having an issue with trees that have null nodes, such as [18, 2, 22, null, null, null, 63, null, 84, null, null]. I'm having an issue with trees that have null nodes, such as [18, 2, 22, null, null, null, 63, null, 84, null, null].

The function seems to stop prematurely. function 似乎过早停止。 If I remove the returns in the first two if blocks, I'm able to stop recursing at the matching val, but unable to get the value to be returned.如果我删除前两个 if 块中的返回,我可以在匹配的 val 处停止递归,但无法获得要返回的值。

How do I alter this algorithm to return the matching val given a BST with null nodes?如何更改此算法以返回给定具有 null 节点的 BST 的匹配 val?

You may consider the following refactored version of visitNode() below.您可以考虑下面的visitNode()重构版本。 The base case occurs when the incoming node be null , in which case the function just returns null .基本情况发生在传入节点是null时,在这种情况下 function 只返回null Otherwise, it checks if the current node matches the sought after value.否则,它会检查当前节点是否与寻求的值匹配。 If not, then it traverses either the left or right recursively.如果不是,则它递归地遍历左或右。

const visitNode = (node, val) => {
    if (node == null) return null;

    if (node.val === val) {
        console.log('MATCH!')
        return node;
    }

    if (val < node.val) {
        return visitNode(node.left, val);
    }
    else {
        return visitNode(node.right, val)
    }
}

If you are looking for binary search tree, try this如果你正在寻找二叉搜索树,试试这个

if (node == null || node.val === val) 
   return node;
 
// val is greater than node's val
if (node->val < val) 
   return visitNode(node.right, val); 

// val is smaller than node's val
return visitNode(node.left, val); 

Or you have to traverse the tree by DFS or BFS或者你必须通过DFSBFS遍历树

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM