Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 求职面试问题变得更难了,检查除根以外的所有问题?_C++_Algorithm_Tree - Fatal编程技术网

C++ 求职面试问题变得更难了,检查除根以外的所有问题?

C++ 求职面试问题变得更难了,检查除根以外的所有问题?,c++,algorithm,tree,C++,Algorithm,Tree,在一次采访中,我遇到了这个问题,虽然一开始当我深入了解这个问题时,它看起来很容易,但对于我来说,面对大量的边缘案例,这似乎是不可能的 要求: 想象一棵树,其中每个节点可以有0到10个子节点,每个子节点都有自己的权重,编写一个函数,执行以下操作: 如果根没有子级,则返回-1 否则,返回树的所有子级(根本身除外)的权重之和 我试过这样的方法: int my_func(node *root) { if (root->isleaf()) { return -1;

在一次采访中,我遇到了这个问题,虽然一开始当我深入了解这个问题时,它看起来很容易,但对于我来说,面对大量的边缘案例,这似乎是不可能的

要求:

想象一棵树,其中每个节点可以有0到10个子节点,每个子节点都有自己的权重,编写一个函数,执行以下操作:

  • 如果根没有子级,则返回-1
  • 否则,返回树的所有子级(根本身除外)的权重之和
  • 我试过这样的方法:

    int my_func(node *root) {
        if (root->isleaf())
        {
            return -1;
        }
        int result = 0;
        for (int i = 0; i < root->num_of_children(); ++i)
        {
            result += my_func(root->child[i]);
        }
        return result;
    }
    
    int my_func(节点*根){
    如果(根->isleaf())
    {
    返回-1;
    }
    int结果=0;
    对于(int i=0;inum_of_children();++i)
    {
    结果+=我的函数(根->子函数[i]);
    }
    返回结果;
    }
    
    但这真的很糟糕,原因有二:

  • 我并不是把所有孩子的体重加起来

  • 当我到达叶子时,我求和-1,而我应该加0


  • 创建一个新函数,该函数将委托给处理根没有子项的情况的函数,并在计算和之后移除根的权重。您还忘记在原始函数中添加权重。您可以通过在添加其余部分之前将
    result
    设置为
    root->weight
    来解决此问题

    int sumOfWeightsExceptRoot(node* root) {
      if (!root || root->isLeaf())
        return -1;
      return my_func(root) - root->weight;
    }
    
    int my_func(node* root) {
      if (!root)
        return 0;
      int result = root->weight;
      for (int i = 0; i < root->num_of_children(); ++i) {
        result += my_func(root->child[i]);
      }
      return result;
    }
    
    int-sumOfWeightsExceptRoot(节点*根){
    如果(!root | root->isLeaf())
    返回-1;
    返回我的函数(根)-根->权重;
    }
    int my_func(节点*根){
    如果(!root)
    返回0;
    int结果=根->重量;
    对于(int i=0;inum_of_children();++i){
    结果+=我的函数(根->子函数[i]);
    }
    返回结果;
    }
    
    迭代版本:

    int sumOfWeightsExceptRoot(node* root) {
      if (!root || root->isLeaf())
        return -1;
    
      std::stack<node*> s;
      s.push(root);
    
      int result = -root->weight;
      while (!s.empty()) {
        node* ptr = s.top(); s.pop();
        result += ptr->weight;
        for (int i = 0; i < ptr->num_of_children(); i++) {
          s.push(ptr->child[i]);
        }
      }
      return result;
    }
    
    int-sumOfWeightsExceptRoot(节点*根){
    如果(!root | root->isLeaf())
    返回-1;
    std::堆栈s;
    s、 推(根);
    int result=-root->weight;
    而(!s.empty()){
    节点*ptr=s.top();s.pop();
    结果+=ptr->重量;
    对于(int i=0;inum_of_children();i++){
    s、 推送(ptr->child[i]);
    }
    }
    返回结果;
    }
    
    创建一个新函数,该函数将委托给处理根没有子项的情况的函数,并在计算和之后删除根的权重。您还忘记在原始函数中添加权重。您可以通过在添加其余部分之前将
    result
    设置为
    root->weight
    来解决此问题

    int sumOfWeightsExceptRoot(node* root) {
      if (!root || root->isLeaf())
        return -1;
      return my_func(root) - root->weight;
    }
    
    int my_func(node* root) {
      if (!root)
        return 0;
      int result = root->weight;
      for (int i = 0; i < root->num_of_children(); ++i) {
        result += my_func(root->child[i]);
      }
      return result;
    }
    
    int-sumOfWeightsExceptRoot(节点*根){
    如果(!root | root->isLeaf())
    返回-1;
    返回我的函数(根)-根->权重;
    }
    int my_func(节点*根){
    如果(!root)
    返回0;
    int结果=根->重量;
    对于(int i=0;inum_of_children();++i){
    结果+=我的函数(根->子函数[i]);
    }
    返回结果;
    }
    
    迭代版本:

    int sumOfWeightsExceptRoot(node* root) {
      if (!root || root->isLeaf())
        return -1;
    
      std::stack<node*> s;
      s.push(root);
    
      int result = -root->weight;
      while (!s.empty()) {
        node* ptr = s.top(); s.pop();
        result += ptr->weight;
        for (int i = 0; i < ptr->num_of_children(); i++) {
          s.push(ptr->child[i]);
        }
      }
      return result;
    }
    
    int-sumOfWeightsExceptRoot(节点*根){
    如果(!root | root->isLeaf())
    返回-1;
    std::堆栈s;
    s、 推(根);
    int result=-root->weight;
    而(!s.empty()){
    节点*ptr=s.top();s.pop();
    结果+=ptr->重量;
    对于(int i=0;inum_of_children();i++){
    s、 推送(ptr->child[i]);
    }
    }
    返回结果;
    }
    
    编写将所有节点、根节点和所有节点的权重相加的函数;这只是一个简单的递归。然后编写另一个调用第一个函数的函数,然后从该总数中减去根的权重(或者,对每个子对象调用第一个函数并将结果相加)。第二个函数还将处理仅根树的特殊情况。c与此有什么关系??我可能会让你面试不及格,因为我不知道有什么不同。另外,关于c++11,您的代码中有哪些具体内容?我看不出任何关联,将删除标记。@πάνταῥεῖ 我相信有更好的方法告诉他C标记不相关。根据文字描述,此函数应始终返回负值。编写将所有节点、根和所有节点的权重相加的函数;这只是一个简单的递归。然后编写另一个调用第一个函数的函数,然后从该总数中减去根的权重(或者,对每个子对象调用第一个函数并将结果相加)。第二个函数还将处理仅根树的特殊情况。c与此有什么关系??我可能会让你面试不及格,因为我不知道有什么不同。另外,关于c++11,您的代码中有哪些具体内容?我看不出任何关联,将删除标记。@πάνταῥεῖ 我相信有更好的方法告诉他C标记不相关。根据字面描述,这个函数应该总是返回一个负值。如果我想在一个函数中包含它们,该怎么办?@mr_calc,那么它应该是一个非递归函数。您可以使用
    std::stack
    并在节点间迭代执行相同的操作。我会发布的。@mr_calc发布了。如果我想把它们放在一个函数中怎么办?@mr_calc则它需要是一个非递归函数。您可以使用
    std::stack
    并在节点间迭代执行相同的操作。“我会把它寄出去的。”卡尔先生发帖说。