【问题标题】:print all nodes at a distance of 'x' from a given node in BST打印与 BST 中给定节点距离为“x”的所有节点
【发布时间】:2014-03-22 19:34:20
【问题描述】:

详细的问题是找到与给定节点的距离为 x(即边数 =x)的所有节点。

今天在亚马逊面试中被问到,

void findNodeWithDistanceX(struct node* root,struct node * qnode, int value)
{
    //root is root Node, qnode is questionnode from which distance to be calculated, value is the            
    //distance to be calculated


    //finding distance between root and qnode
    int distance = findDistancefromRoot(root ,qnode);

    if(distance> value)
    {
        traverseDistancedown(root ,distance-value);
    }

    if(distance ==value){
        printf("%d",root->value);
    }

    // Traverse and find all nodes with distance value from 'qnode' down the tree
    traverseDistancedown(qnode,value);

现在如果从 qnode 找到距离“值”。
我没有得到答案如何遍历树并满足距离与值的条件。

虽然这里出现了很多情况。

我尝试从 qnode 回溯,但无济于事,我无法打动他,也无法编写代码。

任何关于实现向上遍历树的讨论都会对我有所帮助。

【问题讨论】:

  • 广度优先搜索?
  • 对了,请格式化你的代码...希望这不是你在采访中展示的版本

标签: algorithm data-structures tree binary-search-tree


【解决方案1】:

最简单的方法是深度优先搜索,在您前进时跟踪距离。这是一个从“问题节点”开始的简单的前序遍历。

void nodesAtDistance(struct node* qnode, int requestedDistance, int currentDistance)
{
    if (qnode == null) return;

    if (currentDistance == requestedDistance)
    {
        // output qnode
        // and then return because children are further away
        return;
    }

    // visit left and then right
    nodesAtDistance(qnode->left, requestedDistance, currentDistance+1);
    nodesAtDistance(qnode->right, requestedDistance, currentDistance+1);
}

所以如果你这样称呼它:

nodesAtDistance(myNode, 5, 0);

会输出低于myNode的所有5级节点。

现在,如果我将其编写为 API 函数,我将提供一个不需要客户端传递 currentDistance 参数的重载。因为如果有人写nodesAtDistance(myNode, 5, 1),你会在距离 4 处得到东西。所以,创建这个重载:

void nodesAtDistance(node * qnode, int requestedDistance)
{
    nodesAtDistance(qnode, requestedDistance, 0);
}

使该函数公开可见,并将另一个函数设为私有。

【讨论】:

  • 我知道这是旧的,但这并不能回答发帖者提出的问题:“如何遍历树并满足距离与价值的条件?”您只是沿着树向下走,而您还需要在树中找到距离为 k 的节点。
【解决方案2】:

试试这段代码,虽然这段代码只检查子节点并且不会返回检查给定距离的祖先节点

    #include<stdio.h>
    #include<malloc.h>

    struct treenode
    {
     unsigned int data;
     struct treenode *left;
     struct treenode *right;
    };

    struct treenode  *treeptr, *sourcenode, *quesnode, *quesleftnode, *quesrightnode, *root =          NULL;

    struct node *treeptr insert_node(unsigned int treedata)
    {
      treeptr= (struct node*)(malloc(sizeof(treenode)));
      treeptr->data = nodevalue;
      treeptr->left   = NULL;
      treeptr->right = NULL;    
      return treeptr;
   }

   printnodesdistancek(struct treenode* quesnode, unsigned char reqdist)
   {
     unsigned char curdist=0;
     do
     {
      if(quesnode == null)
      return;

      quesleftnode = quesnode->left;

       if(curdist == reqdist)
       {
         printf("%d",quesleftnode->data);
       } 
       else
       {
        quesnode= quesnode->left;
       }
       quesrightnode = quesnode->right;

      if(curdist == reqdist)
      {
        printf("%d",quesrightnode->data);
      }
      else
      {
       quesnode = quesnode->right;
      }
      }while(quesnode!=NULL);
      //main function
      void main(void)
      {
       //create tree

       *root = insert_node(1);
        root->left=insert_node(-1);
        root->right=insert_node(2);
        root->left->left=insert_node(-2);
        root->left->right=insert_node(3);
        root->right->left=insert_node(-3);
        root->right->right=insert_node(4);
        sourcenode = root->left;
        printnodesdistancek(sourcenode,1);

      }

【讨论】:

    猜你喜欢
    • 2012-10-07
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多