给定一个二叉树,每个节点最多可以有两个子节点,任务是找到二叉树的Euler环。欧拉巡回赛由指向树中最高节点的指针表示。如果树为空,则root的值为NULL。
例子:
Input :
Output: 1 5 4 2 4 3 4 5 1
方法:
(1) First, start with root node 1, Euler[0]=1
(2) Go to left node i.e, node 5, Euler[1]=5
(3) Go to left node i.e, node 4, Euler[2]=4
(4) Go to left node i.e, node 2, Euler[3]=2
(5) Go to left node i.e, NULL, go to parent node 4 Euler[4]=4
(6) Go to right node i.e, node 3 Euler[5]=3
(7) No child, go to parent, node 4 Euler[6]=4
(8) All child discovered, go to parent node 5 Euler[7]=5
(9) All child discovered, go to parent node 1 Euler[8]=1
欧拉树的巡回已经讨论过了,它可以应用于邻接表所表示的N元树。如果用链接和节点的经典结构化方式表示一棵二叉树,则需要先将其转换为邻接表表示,然后,如果我们想应用原始文章中讨论的方法,则可以找到Euler环。但这增加了程序的空间复杂度。在此,本文讨论了一种广义的空间优化版本,该版本可以直接应用于以结构节点表示的二叉树。
此方法:
(1)无需使用Visited数组即可工作。
(2)恰好需要2 * N-1个顶点来存储Euler巡回。
C++
// C++ program to find euler tour of binary tree
#include
using namespace std;
/* A tree node structure */
struct Node {
int data;
struct Node* left;
struct Node* right;
};
/* Utility function to create a new Binary Tree node */
struct Node* newNode(int data)
{
struct Node* temp = new struct Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// Find Euler Tour
void eulerTree(struct Node* root, vector &Euler)
{
// store current node's data
Euler.push_back(root->data);
// If left node exists
if (root->left)
{
// traverse left subtree
eulerTree(root->left, Euler);
// store parent node's data
Euler.push_back(root->data);
}
// If right node exists
if (root->right)
{
// traverse right subtree
eulerTree(root->right, Euler);
// store parent node's data
Euler.push_back(root->data);
}
}
// Function to print Euler Tour of tree
void printEulerTour(Node *root)
{
// Stores Euler Tour
vector Euler;
eulerTree(root, Euler);
for (int i = 0; i < Euler.size(); i++)
cout << Euler[i] << " ";
}
/* Driver function to test above functions */
int main()
{
// Constructing tree given in the above figure
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->right->left->right = newNode(8);
// print Euler Tour
printEulerTour(root);
return 0;
}
Java
// Java program to find euler tour of binary tree
import java.util.*;
class GFG
{
/* A tree node structure */
static class Node
{
int data;
Node left;
Node right;
};
/* Utility function to create a new Binary Tree node */
static Node newNode(int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null;
return temp;
}
// Find Euler Tour
static Vector eulerTree(Node root, Vector Euler)
{
// store current node's data
Euler.add(root.data);
// If left node exists
if (root.left != null)
{
// traverse left subtree
Euler = eulerTree(root.left, Euler);
// store parent node's data
Euler.add(root.data);
}
// If right node exists
if (root.right != null)
{
// traverse right subtree
Euler = eulerTree(root.right, Euler);
// store parent node's data
Euler.add(root.data);
}
return Euler;
}
// Function to print Euler Tour of tree
static void printEulerTour(Node root)
{
// Stores Euler Tour
Vector Euler = new Vector();
Euler = eulerTree(root, Euler);
for (int i = 0; i < Euler.size(); i++)
System.out.print(Euler.get(i) + " ");
}
/* Driver function to test above functions */
public static void main(String[] args)
{
// Constructing tree given in the above figure
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.right.left.right = newNode(8);
// print Euler Tour
printEulerTour(root);
}
}
// This code is contributed by Rajput-Ji
C#
// C# program to find euler tour of binary tree
using System;
using System.Collections.Generic;
class GFG
{
/* A tree node structure */
public class Node
{
public int data;
public Node left;
public Node right;
};
/* Utility function to create a new Binary Tree node */
static Node newNode(int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null;
return temp;
}
// Find Euler Tour
static List eulerTree(Node root, List Euler)
{
// store current node's data
Euler.Add(root.data);
// If left node exists
if (root.left != null)
{
// traverse left subtree
Euler = eulerTree(root.left, Euler);
// store parent node's data
Euler.Add(root.data);
}
// If right node exists
if (root.right != null)
{
// traverse right subtree
Euler = eulerTree(root.right, Euler);
// store parent node's data
Euler.Add(root.data);
}
return Euler;
}
// Function to print Euler Tour of tree
static void printEulerTour(Node root)
{
// Stores Euler Tour
List Euler = new List();
Euler = eulerTree(root, Euler);
for (int i = 0; i < Euler.Count; i++)
Console.Write(Euler[i] + " ");
}
/* Driver function to test above functions */
public static void Main(String[] args)
{
// Constructing tree given in the above figure
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.right.left.right = newNode(8);
// print Euler Tour
printEulerTour(root);
}
}
// This code is contributed by 29AjayKumar
1 2 4 2 5 2 1 3 6 8 6 3 7 3 1
时间复杂度: O(2 * N-1),其中N是树中的节点数。
辅助空间: O(2 * N-1),其中N是树中的节点数。