Given a binary tree where each node can have at most two child nodes, the task is to find the Euler tour of the binary tree. Euler tour is represented by a pointer to the topmost node in the tree. If the tree is empty, then value of root is NULL.
Output: 1 5 4 2 4 3 4 5 1
(1) First, start with root node 1, Euler=1
(2) Go to left node i.e, node 5, Euler=5
(3) Go to left node i.e, node 4, Euler=4
(4) Go to left node i.e, node 2, Euler=2
(5) Go to left node i.e, NULL, go to parent node 4 Euler=4
(6) Go to right node i.e, node 3 Euler=3
(7) No child, go to parent, node 4 Euler=4
(8) All child discovered, go to parent node 5 Euler=5
(9) All child discovered, go to parent node 1 Euler=1
Euler tour of tree has been already discussed where it can be applied to N-ary tree which is represented by adjacency list. If a Binary tree is represented by the classical structured way by links and nodes, then there need to first convert the tree into adjacency list representation and then we can find the Euler tour if we want to apply method discussed in the original post. But this increases the space complexity of the program. Here, In this post, a generalized space-optimized version is discussed which can be directly applied to binary trees represented by structure nodes.
This method :
(1) Works without the use of Visited arrays.
(2) Requires exactly 2*N-1 vertices to store Euler tour.
1 2 4 2 5 2 1 3 6 8 6 3 7 3 1
Time Complexity: O(2*N-1) where N is number of nodes in the tree.
Auxiliary Space : O(2*N-1) where N is number of nodes in the tree.
- Euler Tour of Tree
- Euler Tour | Subtree Sum using Segment Tree
- Total nodes traversed in Euler Tour Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Minimum swap required to convert binary tree to binary search tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Check whether a given binary tree is skewed binary tree or not?
- Difference between Binary Tree and Binary Search Tree
- Binary Tree to Binary Search Tree Conversion using STL set
- Check whether a binary tree is a full binary tree or not
- Binary Tree to Binary Search Tree Conversion
- Check if a binary tree is subtree of another binary tree | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.