无法理解树遍历递归函数

时间:2015-11-20 04:04:23

标签: recursion binary-tree binary-search-tree preorder

我在理解preorder,inorder和postorder树遍历中涉及的递归函数时遇到了一些麻烦。我有一些递归的知识(但不可否认它不是我的强项)。所有这些人似乎都称自己两次先与根的左子女打电话,然后与正确的孩子打电话。但这究竟是怎么可能的呢?不会调用preOrder函数,左子项将控制流返回到顶部,下一次调用永远不会被执行?

void preOrder (Node* root) 
{
    if (root == NULL) return;
    cout<<root->val<<", ";
    preOrder(root->left);
    preOrder(root->right);
}

3 个答案:

答案 0 :(得分:1)

使用左子项调用preOrder函数是不是会将控制流程返回到顶部,而下一次调用将永远不会被执行?

当然不是。递归调用就像任何其他函数调用一样:在函数完成后,控件返回到调用位置。 &#39;地点&#39;不仅意味着代码中的点,还意味着调用堆栈上的点,这意味着可以再次使用相同的变量集。就像从任何其他功能返回后一样。

例如,如果你有一棵树

func handleSwipes(sender:UISwipeGestureRecognizer) {

   // up or down
   if sender.direction == .Up {
    increment = 1
    offset = 10
   } else {
    increment = -1
    offset = -10
   }

  // animate stuff with constraints
  inc(increment)

  UIView.animateWithDuration(0.18, animations: { _ in

    self.labelYConstraint.constant = self.offset
    self.view.layoutIfNeeded()
    self.label.alpha = 1
    self.label.textColor = UIColor(red: 52/255.0, green: 52/255.0, blue: 88/255.0, alpha: 1)
    self.circleView.filledColor = UIColor(red: 167/255.0, green: 246/255.0, blue: 67/255.0, alpha: 1)
  }
  }) { _ in

    UIView.animateWithDuration(0.18, animations: { _ in
      self.labelYConstraint.constant = 0
      self.view.layoutIfNeeded()
      self.circleView.layer.backgroundColor = UIColor(red: 211/255.0, green: 211/255.0, blue: 211/255.0, alpha: 0.3).CGColor
      self.label.textColor = UIColor.whiteColor()

    })
 }
}

然后在 A / \ X Y 节点上调用preorder,然后首先打印A内容,然后在A上调用preorder并返回回到X,然后继续致电preorder(A)上的preorder

答案 1 :(得分:0)

预购:处理节点,移至左侧儿童,移至右侧儿童

void preorder(Node *root)
{
    if (root == NULL) //<- Check if the currently passed node is empty
        return; //<- if it is, return from the function back down the stack

    cout << root->val << ", "; //<- if it wasn't empty, print its value

    if (root->left != NULL) //<- check if the node to the left is empty
        preorder(root->left); //<- if not recurse to the left child

    if (root->right != NULL) //<- check if the node to the right is empty
        preorder(root->right); //<- if not recurse to the right child
}

按顺序:向左移动,处理节点,向右移动

void inorder(Node *root)
{
    if (root == NULL)
        return;

    if (root->left != NULL) //<- check if the node to the left is empty
            inorder(root->left); //<- if not recurse to the left child

    cout << root->val << ", ";

    if (root->right != NULL) //<- check if the node to the right is empty
            inorder(root->right); //<- if not recurse to the right child
}

延期交货:移至左侧节点,移至右侧节点,处理节点

void postorder(Node *root)
{
    if (root == NULL)
        return;

    if (root->left != NULL) //<- check if the node to the left is empty
            postorder(root->left); //<- if not recurse to the left child

    if (root->right != NULL) //<- check if the node to the right is empty
            postorder(root->right); //<- if not recurse to the right child

    cout << root->val << ", "
}

答案 2 :(得分:0)

void preorder(node *p) {
   if(p) {
     cout << p->val <<"\n";
     preorder(p->left);
     preorder(p->right);
   }
}