Morris Inorder Traversal

Binary Trees Traversal in Constant Space Hard
  • Fun Fact: The underlying concept of an Inorder Tree Traversal, including the optimized Morris Inorder Traversal, has extensive practical applications in database systems
  • Database storage and querying often utilize B-trees and B+ trees, which are generalized binary trees
  • Thus, Inorder Traversal is used to retrieve items from these trees in a sorted manner
  • This is crucial for operations like range queries or sorted output which forms the backbone of database manipulation languages like SQL

Given root of binary tree, return the Inorder traversal of the binary tree.


Morris Inorder Traversal is a tree traversal algorithm aiming to achieve a space complexity of O(1) without recursion or an external data structure.

Examples:

Input : root = [1, 4, null, 4, 2]

Output : [4, 4, 2, 1]

Explanation :

Input : root = [1, null, 2, 3]

Output : [1, 3, 2]

Explanation :

Input : root = [5, 1, 2, 8, null, 4, 5, null, 6]

Constraints

  • 1 <= Number of Nodes <= 100
  • -100 <= Node.val <= 100

Hints

  • Instead of using a stack, it modifies the tree structure temporarily by creating threaded links using the rightmost node of the left subtree.
  • Instead of using a stack, it modifies the tree structure temporarily by creating threaded links using the rightmost node of the left subtree.

Company Tags

Epic Systems Visa Zoho Shopify Lyft Ernst & Young Morgan Stanley Western Digital GE Healthcare Salesforce eBay Unity Technologies Stripe Qualcomm Robinhood Swiggy MongoDB Byju's Boston Consulting Group Cloudflare Oracle Flipkart IBM Cerner Bloomberg Google Microsoft Amazon Meta Apple Netflix Adobe