Data structures 从右向左打印二叉树中的叶节点?

Data structures 从右向左打印二叉树中的叶节点?,data-structures,tree,binary-tree,Data Structures,Tree,Binary Tree,我正在寻找一个答案: 从中查找在二叉树中打印叶节点的伪代码 从右到左 我很高兴听到一些想法。一个提示(当然不是完整的解决方案)或一个相关主题的链接可以帮助我理解这个问题。你需要使用递归方法,首先将该方法传递给二叉树的顶级节点 在伪代码中,我假设每个节点都定义了“right”和“left”成员,它们本身就是节点和一个“name”属性,以打印有关节点的内容。该方法可能是这样的,在没有特定语言的情况下,因为您说的是伪代码: function processNode(parent) { if(p

我正在寻找一个答案:

从中查找在二叉树中打印叶节点的伪代码 从右到左


我很高兴听到一些想法。一个提示(当然不是完整的解决方案)或一个相关主题的链接可以帮助我理解这个问题。你需要使用递归方法,首先将该方法传递给二叉树的顶级节点

在伪代码中,我假设每个节点都定义了“right”和“left”成员,它们本身就是节点和一个“name”属性,以打印有关节点的内容。该方法可能是这样的,在没有特定语言的情况下,因为您说的是伪代码:

function processNode(parent) {
    if(parent.right = null AND parent.left = null)
        print parent.name
    if(parent.right <> null)
        processNode(parent.right)
    if(parent.left <> null)
        processNode(parent.left)
}

您需要使用递归方法,首先将该方法传递给二叉树的顶级节点

在伪代码中,我假设每个节点都定义了“right”和“left”成员,它们本身就是节点和一个“name”属性,以打印有关节点的内容。该方法可能是这样的,在没有特定语言的情况下,因为您说的是伪代码:

function processNode(parent) {
    if(parent.right = null AND parent.left = null)
        print parent.name
    if(parent.right <> null)
        processNode(parent.right)
    if(parent.left <> null)
        processNode(parent.left)
}

对树执行深度优先遍历,首先处理右子树,然后仅打印叶节点

实现这一点的最简单方法是使用递归函数

void printLeafNodes(二进制TreeNode*treePtr){
if(treePtr.leftChild==null&&treePtr.rightChild==null){
//这是叶节点;打印其值
}否则{
//在右子树上递归
if(treePtr.rightChild!=null){
printLeafNodes(treePtr.rightChild);
}
//在左子树上递归
if(treePtr.leftChild!=null){
printLeafNodes(treepr.leftChild);
}
}
}

此页面对于可视化解决方案非常有用:。

执行树的深度优先遍历,首先处理正确的子树,然后仅打印叶节点

实现这一点的最简单方法是使用递归函数

void printLeafNodes(二进制TreeNode*treePtr){
if(treePtr.leftChild==null&&treePtr.rightChild==null){
//这是叶节点;打印其值
}否则{
//在右子树上递归
if(treePtr.rightChild!=null){
printLeafNodes(treePtr.rightChild);
}
//在左子树上递归
if(treePtr.leftChild!=null){
printLeafNodes(treepr.leftChild);
}
}
}

此页面对于可视化解决方案非常有帮助:。

我知道我正在恢复一个旧线程,但我认为它可能有助于其他人查看此线程。如果你谈论的是面试问题,我认为递归答案只是答案的一小部分,面试官也会喜欢听迭代方法。从右向左遍历(打印)不是wiki中描述的常规前/后/顺序遍历。这里的主要思想是,您需要尽可能地向右移动,首先从最右边的节点开始打印,然后从其父节点开始打印,然后从左子树开始打印

递归:

printRightToLeft(Node root){
  if (root == null)
     return;

  // You can check root.right and root.left for null before calling the
  // printRightToLeft function to avoid pushing unneeded data to recursion
  // call stack.

  printRightToLeft(root.right);
  if (root.right == null && root.left == null)
     print(root);
  printRightToLeft(root.left);
}
迭代:

printRightToLeft(Node root){
  if (root == null)
    return;

  Stack s = new Stack();
  s.push(root);

  while(!s.isEmpty()){
    Node n = s.top();

    if (n.right != null && !n.visited){
      s.push(n.right);
      n.visited = true;
    } else {
      s.pop();

      if (n.right == null && n.left == null)
         print(n);

      if (n.left != null)
         s.push(n.left);
    }
}

我知道我正在复活一个旧的线程,但我认为它可能会帮助其他人查看这个线程。如果你谈论的是面试问题,我认为递归答案只是答案的一小部分,面试官也会喜欢听迭代方法。从右向左遍历(打印)不是wiki中描述的常规前/后/顺序遍历。这里的主要思想是,您需要尽可能地向右移动,首先从最右边的节点开始打印,然后从其父节点开始打印,然后从左子树开始打印

递归:

printRightToLeft(Node root){
  if (root == null)
     return;

  // You can check root.right and root.left for null before calling the
  // printRightToLeft function to avoid pushing unneeded data to recursion
  // call stack.

  printRightToLeft(root.right);
  if (root.right == null && root.left == null)
     print(root);
  printRightToLeft(root.left);
}
迭代:

printRightToLeft(Node root){
  if (root == null)
    return;

  Stack s = new Stack();
  s.push(root);

  while(!s.isEmpty()){
    Node n = s.top();

    if (n.right != null && !n.visited){
      s.push(n.right);
      n.visited = true;
    } else {
      s.pop();

      if (n.right == null && n.left == null)
         print(n);

      if (n.left != null)
         s.push(n.left);
    }
}
  • 执行顺序遍历并仅将叶节点添加到已访问节点列表中
  • 恢复列表
  • 实际上,在第一步中创建列表时,在头部添加节点,并让它指向前一个节点(而不是指向新节点的前一个节点)

  • 执行顺序遍历并仅将叶节点添加到已访问节点列表中
  • 恢复列表

  • 实际上,在第一步中创建列表时,在头部添加节点,并让它指向前一个节点(而不是指向新节点的前一个节点)

    在执行级别顺序遍历时以相反顺序打印叶节点,不包括非叶节点。

    在执行级别顺序遍历时以相反顺序打印叶节点,不包括非叶节点。

    先执行InOrder/Preorder/postorder,但对于右子级,然后转到左子级

    void postOrder(Node* root)
    {
    if(root==NULL)
    return;
    postOrder(root->right);
    postOrder(root->left);
    if(root->left==NULL && root->right==NULL)
    cout << root->data <<" ";
    }
    
    void postOrder(节点*根)
    {
    if(root==NULL)
    返回;
    后订单(根->右);
    后订单(根->左);
    if(root->left==NULL&&root->right==NULL)
    
    cout dataINORDER/Preorder/POSTERORDER,但先对右儿童进行排序,然后再对左儿童进行排序

    void postOrder(Node* root)
    {
    if(root==NULL)
    return;
    postOrder(root->right);
    postOrder(root->left);
    if(root->left==NULL && root->right==NULL)
    cout << root->data <<" ";
    }
    
    void postOrder(节点*根)
    {
    if(root==NULL)
    返回;
    后订单(根->右);
    后订单(根->左);
    if(root->left==NULL&&root->right==NULL)
    cout data
    void in(节点*根){
    如果(根)
    {   
    如果(!root->left&!root->right)
    cout
    void in(节点*根){
    如果(根)
    {   
    如果(!root->left&!root->right)
    
    想必您知道如何使用递归按顺序遍历二叉树

    void visit(Node node) {
         if(node.hasLeft()) {
             visit(node.getLeft());
         }
         handleValue(node.value); // print or whatever
         if(node.hasRight()) {
             visit(node.getRight());
         }
    }
    
    您会注意到,在执行此操作时,除了处理非叶节点外,您已经按照从左到右的顺序处理了叶节点

    要从右向左访问,只需颠倒语句的顺序——访问右侧,然后处理值,然后访问左侧


    要仅打印叶节点,只需在
    handleValue
    周围放置一个
    if
    语句,告诉它仅在节点是叶节点时输出。如果节点既没有左子节点也没有右子节点,则节点是叶节点。

    大概您知道如何使用递归按顺序遍历二叉树

    void visit(Node node) {
         if(node.hasLeft()) {
             visit(node.getLeft());
         }
         handleValue(node.value); // print or whatever
         if(node.hasRight()) {
             visit(node.getRight());
         }
    }
    
    您会注意到,当您这样做时,除了handl之外,您已经在按从左到右的顺序处理树叶了