Inorder Traversal of Binary Tree in Java

🔥 Day 97 of my 100 Days of Code Problem: Binary Tree Inorder Traversal (LeetCode #94) Problem Description: Given the root of a binary tree, return the inorder traversal of its nodes’ values. Inorder means visiting nodes in this order: Left -> Node -> Right Java Solution: class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode current = root; while (current != null || !stack.isEmpty()) { // Reach the leftmost node while (current != null) { stack.push(current); current = current.left; } // Process node current = stack.pop(); result.add(current.val); // Visit right subtree current = current.right; } return result; } } Time Complexity: O(n) Space Complexity: O(n) (for stack in the worst case) #Day97 #LeetCode #100DaysOfCode #Java #BinaryTree #DFS

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories