简体   繁体   English

使用递归DFS在二叉树中查找节点

[英]Using recursive DFS to find node in binary tree

I have to use DFS to search a binary tree to find a node.我必须使用 DFS 搜索二叉树才能找到节点。 (tok is the string I'm searching for). (tok 是我正在搜索的字符串)。 If it finds it, it has to return the number of nodes it traversed to find it.如果找到它,它必须返回它为找到它而遍历的节点数。 If it doesn't then it has to return -1.如果不是,那么它必须返回-1。

I have tried many recursive solutions but honestly, I'm stumped.我尝试了很多递归解决方案,但老实说,我很难过。 I may not be returning values correctly.我可能没有正确返回值。

Test case: Lets say i have a tree with the root called "John"."John" as a left child "Shayne" and a right child "Eric".测试用例:假设我有一棵树,根名为“John”。“John”作为左孩子“Shayne”和右孩子“Eric”。 Additionally, "Shayne" has a left child "Max".此外,“Shayne”有一个左孩子“Max”。 The output would be correct for John,Shayne and Max.输出对于 John、Shayne 和 Max 来说是正确的。 But the output of Eric should be 4, since i traverse john and then shayne and then max and then Eric (considering im going left first and then right), but for Eric, im getting the output of 3但是 Eric 的输出应该是 4,因为我遍历 john 然后 shayne 然后 max 然后 Eric(考虑我先向左然后向右),但是对于 Eric,我得到了 3 的输出

Edited with exact test case.使用精确的测试用例进行编辑。

#include <stdio.h>

#include <stdlib.h>
#include <string.h>

struct node {
    char* name;
    char* tea;
    struct node* left;
    struct node* right;
};

typedef struct node Node;

int depth(struct node* root);
int dfs(struct node* root, char* tok);

int main() {

    Node* John = (Node*)malloc(sizeof(Node));
    John->left = NULL;
    John->right = NULL;
    John->name = "John";
    Node* Shayne = (Node*)malloc(sizeof(Node));
    Shayne->left = NULL;
    Shayne->right = NULL;
    Shayne->name = "Shayne";
    Node* Eric = (Node*)malloc(sizeof(Node));
    Eric->left = NULL;
    Eric->right = NULL;
    Eric->name = "Eric";
    Node* Max = (Node*)malloc(sizeof(Node));
    Max->left = NULL;
    Max->right = NULL;
    Max->name = "Max";

    John->left = Shayne;
    Shayne->left = Max;
    John->right = Eric;

    printf("%d",dfs(John,"Eric"));
}

int depth(struct node* root) {


    if (root == NULL) {
        return 0;
    }
    int l = depth(root->left);
    int r = depth(root->right);
    int d = max(l, r) + 1;
    return d;



}


int dfs(struct node* root, char* tok) {


    if (root == NULL) {
        return 0;
    }
    if (strcmp(root->name, tok) == 0) {
        return 1;
    }
    else {
        int l = dfs(root->left, tok);
        if (l != -1) {
            return 1 + l;
        }
        int r = dfs(root->right, tok);
        if (r != -1) {
            return 1+l+ r;
        }
        return -1;
    }
}

You correctly add 1 to the return value when the value has been found in an immediate child to produce the number of nodes.当在直接子节点中找到该值以生成节点数时,您正确地将 1 添加到返回值。 But it also means that you will return 2 to your parent.但这也意味着您将返回2给您的父母。

You have to change your test to您必须将测试更改为

if (l != -1) { //found on left child
        return 1 + l;
    }

The only problem with your function is that when you are returning from the child node you are always checking the value of l with 1 for instance:你的函数唯一的问题是,当你从子节点返回时,你总是用 1 检查 l 的值,例如:

int l = dfs(root->left, tok);
    if (l == 1) { //found on left child
        return 1 + l;
    }

which will work fine for the first 2 nodes but then the value of the return becomes 2,3,4,.... in that case it will skip the if and return -1 again, so to solving this problem a good approach will be to check if the return value is not -1, for example:这对于前 2 个节点可以正常工作,但是返回的值变为 2,3,4,.... 在这种情况下,它将跳过 if 并再次返回 -1,因此解决这个问题的好方法是是检查返回值是否不是-1,例如:

int l = dfs(root->left, string);
    if (l != -1) { 
        return 1 + l;
    }
    int r = dfs(root->right, string);
    if (r != -1) { 
        return 1 + r;
    }

Hope this gives you the answer.希望这能给你答案。

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

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