📜  二叉树的三阶遍历

📅  最后修改于: 2021-09-06 06:49:50             🧑  作者: Mango

给定一个二叉树,任务是找到它的三重遍历

例子:

Input:
            A
           / \
          B   C
         / \   \
        F   D   E
       
Output: A B F F F B D D D B A C C E E E C A

Input:
            A
           / \
          B   C
         / \   
        E   D   
       /
      F
Output: A B E F F F E E B D D D B A C C C A 

方法:
请按照以下步骤解决问题:

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

下面是上述方法的实现:

C++
// C++ Program to implement triple
// order traversal of a binary tree
#include 
using namespace std;
  
// Structure of a Node
struct node {
    char data;
    struct node *left, *right;
};
  
// Function to create new node
struct node* newNode(char ch)
{
    // Allocating a new node in the memory.
    struct node* n = (struct node*)
        malloc(sizeof(struct node));
    n->data = ch;
    n->left = NULL;
    n->right = NULL;
    return n;
}
  
// Function to print Triple Order traversal
void tripleOrderTraversal(struct node* root)
{
    if (root) {
  
        // Print the current node
        cout << root->data << " ";
  
        // Traverse left subtree
        tripleOrderTraversal(root->left);
  
        // Print the current node
        cout << root->data << " ";
  
        // Traverse right subtree
        tripleOrderTraversal(root->right);
  
        // Print the current node
        cout << root->data << " ";
    }
}
  
// Driver Code
int main()
{
    struct node* root = newNode('A');
    root->left = newNode('B');
    root->right = newNode('C');
    root->left->left = newNode('F');
    root->left->right = newNode('D');
    root->right->right = newNode('E');
  
    tripleOrderTraversal(root);
}


Java
// Java program to implement triple
// order traversal of a binary tree
import java.util.*;
  
class GFG{
  
// Structure of a Node
static class node
{
    char data;
    node left, right;
};
  
// Function to create new node
static node newNode(char ch)
{
      
    // Allocating a new node in the memory.
    node n = new node();
    n.data = ch;
    n.left = null;
    n.right = null;
    return n;
}
  
// Function to print Triple Order traversal
static void tripleOrderTraversal(node root)
{
    if (root != null)
    {
          
        // Print the current node
        System.out.print(root.data + " ");
  
        // Traverse left subtree
        tripleOrderTraversal(root.left);
  
        // Print the current node
        System.out.print(root.data + " ");
  
        // Traverse right subtree
        tripleOrderTraversal(root.right);
  
        // Print the current node
        System.out.print(root.data + " ");
    }
}
  
// Driver Code
public static void main(String[] args)
{
    node root = newNode('A');
    root.left = newNode('B');
    root.right = newNode('C');
    root.left.left = newNode('F');
    root.left.right = newNode('D');
    root.right.right = newNode('E');
  
    tripleOrderTraversal(root);
}
}
  
// This code is contributed by amal kumar choubey


Python3
# Python3 program to implement triple
# order traversal of a binary tree
  
# Structure of node
class Node:
      
    # Initialise the node
    def __init__(self, ch):
          
        self.data = ch
        self.left = None
        self.right = None
          
# Function to print the Triple Order Traversal
def tripleOrderTraversal(root):
      
    if root:
          
        # Print the current node
        print(root.data, end = ' ')
          
        # Print the left subtree
        tripleOrderTraversal(root.left)
          
        # Print the current node
        print(root.data, end = ' ')
          
        # Print the right subtree
        tripleOrderTraversal(root.right)
          
        # Print the current node
        print(root.data, end = ' ')
          
# Driver code
root = Node('A')
root.left = Node('B')
root.right = Node('C')
root.left.left = Node('F')
root.left.right = Node('D')
root.right.right = Node('E')
  
tripleOrderTraversal(root)
      
# This code is contributed by Stuti Pathak


C#
// C# program to implement triple
// order traversal of a binary tree
using System;
  
class GFG{
  
// Structure of a Node
public class node
{
    public char data;
    public node left, right;
};
  
// Function to create new node
static node newNode(char ch)
{
      
    // Allocating a new node in the memory.
    node n = new node();
    n.data = ch;
    n.left = null;
    n.right = null;
    return n;
}
  
// Function to print Triple Order traversal
static void tripleOrderTraversal(node root)
{
    if (root != null)
    {
          
        // Print the current node
        Console.Write(root.data + " ");
  
        // Traverse left subtree
        tripleOrderTraversal(root.left);
  
        // Print the current node
        Console.Write(root.data + " ");
  
        // Traverse right subtree
        tripleOrderTraversal(root.right);
  
        // Print the current node
        Console.Write(root.data + " ");
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    node root = newNode('A');
    root.left = newNode('B');
    root.right = newNode('C');
    root.left.left = newNode('F');
    root.left.right = newNode('D');
    root.right.right = newNode('E');
  
    tripleOrderTraversal(root);
}
}
  
// This code is contributed by amal kumar choubey


输出:
A B F F F B D D D B A C C E E E C A

时间复杂度: O(N)
辅助空间: O(1)
应用:一棵树的欧拉图是三阶遍历的修改版本。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live