Morris Inorder Traversal

Binary Trees Traversal in Constant Space Hard

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