Fetching the code, complexity notes and tags. Just a moment.
This snippet demonstrates how to perform a Depth-First Search (DFS) traversal on a binary tree using recursion. It uses the Inorder Traversal technique, where the left subtree is visited first, followed by the root node, and then the right subtree. DFS is a fundamental tree traversal algorithm used in binary trees, binary search trees, expression trees, and many interview problems.
Category
trees
Complexity
Time: O(n) | Space: O(h)
Language
Java
Status
Tags
Related Concepts
Published
Last Updated
Production-ready Java implementation for quick revision, interview preparation, and real-world development.
Solution.java
1class TreeNode {2 3 int value;4 TreeNode left;5 TreeNode right;6 7 TreeNode(int value) {8 this.value = value;9 }10}11 12public class BinaryTreeDFS {13 14 public static void inorderTraversal(TreeNode root) {15 16 if (root == null) {17 return;18 }19 20 inorderTraversal(root.left);21 22 System.out.print(root.value + " ");23 24 inorderTraversal(root.right);25 }26 27 public static void main(String[] args) {28 29 TreeNode root = new TreeNode(1);30 31 root.left = new TreeNode(2);32 root.right = new TreeNode(3);33 34 root.left.left = new TreeNode(4);35 root.left.right = new TreeNode(5);36 37 root.right.left = new TreeNode(6);38 root.right.right = new TreeNode(7);39 40 System.out.println("Inorder DFS Traversal:");41 42 inorderTraversal(root);43 }44}Keywords
Explore the important concepts and keywords associated with this Java snippet.
💡 These tags help you quickly identify the concepts covered in this snippet and make it easier to discover similar Java solutions throughout the library.