📜  二叉树的双阶遍历

📅  最后修改于: 2021-04-29 10:07:07             🧑  作者: Mango

给定一个由N个节点组成的二叉树,任务是打印其双阶遍历。

例子:

Input:
        1
      /   \
     7     3
    / \   /
   4   5 6
Output: 1 7 4 4 7 5 5 1 3 6 6 3 

Input:
        1
      /   \
     7     3
    / \     \
   4   5     6
Output: 1 7 4 4 7 5 5 1 3 3 6 6



方法:
这个想法是在给定的二叉树上递归地执行有序遍历,并且在遍历过程中在访问顶点时以及在对左子树的递归调用之后打印节点值。
请按照以下步骤解决问题:

  • 开始顺序遍历
  • 如果当前节点不存在,只需从其返回即可。
  • 除此以外:
    • 打印当前节点的值。
    • 递归遍历左子树。
    • 再次打印当前节点
    • 递归遍历右边的子树。
  • 重复上述步骤,直到访问树中的所有节点。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above appraoch
#include 
using namespace std;
 
// Node Structure
struct node {
    char data;
    struct node *left, *right;
};
 
// Function to create new node
struct node* newNode(char ch)
{
    // Allocate a new node in memory
    struct node* n = (struct node*)
        malloc(sizeof(struct node));
    n->data = ch;
    n->left = NULL;
    n->right = NULL;
    return n;
}
 
// Function to print Double Order traversal
void doubleOrderTraversal(struct node* root)
{
    if (!root)
        return;
 
    // Print Node Value
    cout << root->data << " ";
 
    // Traverse Left Subtree
    doubleOrderTraversal(root->left);
 
    // Print Node Value
    cout << root->data << " ";
 
    // Traverse Right SubTree
    doubleOrderTraversal(root->right);
}
 
// Driver Code
int main()
{
    struct node* root = newNode('1');
    root->left = newNode('7');
    root->right = newNode('3');
    root->left->left = newNode('4');
    root->left->right = newNode('5');
    root->right->right = newNode('6');
 
    doubleOrderTraversal(root);
    return 0;
}


Java
// Java program to implement
// the above appraoch
class GFG{
 
// Node Structure
static class node
{
    char data;
    node left, right;
};
 
// Function to create new node
static node newNode(char ch)
{
     
    // Allocate a new node in memory
    node n = new node();
    n.data = ch;
    n.left = null;
    n.right = null;
    return n;
}
 
// Function to print Double Order traversal
static void doubleOrderTraversal(node root)
{
    if (root == null)
        return;
 
    // Print Node Value
    System.out.print(root.data + " ");
 
    // Traverse Left Subtree
    doubleOrderTraversal(root.left);
 
    // Print Node Value
    System.out.print(root.data + " ");
 
    // Traverse Right SubTree
    doubleOrderTraversal(root.right);
}
 
// Driver Code
public static void main(String[] args)
{
    node root = newNode('1');
    root.left = newNode('7');
    root.right = newNode('3');
    root.left.left = newNode('4');
    root.left.right = newNode('5');
    root.right.right = newNode('6');
 
    doubleOrderTraversal(root);
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program to implement
# the above appraoch
 
# Node Structure
class Node:
 
    # Initialise new node
    def __init__(self, ch):
         
        self.data = ch
        self.left = None
        self.right = None
 
# Function to print Double Order traversal
def doubleOrderTraveersal(root):
     
    if not root:
        return
 
    # Print node value
    print(root.data, end = " ")
 
    # Traverse left subtree
    doubleOrderTraveersal(root.left)
 
    # Print node value
    print(root.data, end = " ")
 
    # Traverse right subtree
    doubleOrderTraveersal(root.right)
 
# Driver code
if __name__ == '__main__':
 
    root = Node(1)
    root.left = Node(7)
    root.right = Node(3)
    root.left.left = Node(4)
    root.left.right = Node(5)
    root.right.right = Node(6)
     
    doubleOrderTraveersal(root)
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above appraoch
using System;
class GFG{
 
// Node Structure
class node
{
    public char data;
    public node left, right;
};
 
// Function to create new node
static node newNode(char ch)
{
     
    // Allocate a new node in memory
    node n = new node();
    n.data = ch;
    n.left = null;
    n.right = null;
    return n;
}
 
// Function to print Double Order traversal
static void doubleOrderTraversal(node root)
{
    if (root == null)
        return;
 
    // Print Node Value
    Console.Write(root.data + " ");
 
    // Traverse Left Subtree
    doubleOrderTraversal(root.left);
 
    // Print Node Value
    Console.Write(root.data + " ");
 
    // Traverse Right SubTree
    doubleOrderTraversal(root.right);
}
 
// Driver Code
public static void Main(String[] args)
{
    node root = newNode('1');
    root.left = newNode('7');
    root.right = newNode('3');
    root.left.left = newNode('4');
    root.left.right = newNode('5');
    root.right.right = newNode('6');
 
    doubleOrderTraversal(root);
}
}
 
// This code is contributed by gauravrajput1


输出:
1 7 4 4 7 5 5 1 3 3 6 6



时间复杂度: O(N)
辅助空间: O(1)