How to implement Level Order Traversal of Binary Tree in Java? Example Tutorial

Hello guys, if you have worked in Java then you know that binary tree is one of the essential data structure and quite an important one for programmers, I even mentioned that on my 10 essential data structures for programmers articles.  Binary tree related question are also quite common on coding interviews and we are going to see one today but before that, let's revise what is binary tree? Binary trees are hierarchical data structures composed of nodes, each having at most two children: a left child and a right child. Traversing a binary tree in level order involves visiting nodes level by level, starting from the root. 

How to Implement Binary Tree InOrder traversal in Java without Recursion - Example Tutorial

I have been writing about different binary tree traversal algorithms and so far we have seen both pre-order and post-order algorithms to traverse a binary tree and today you'll learn about the in-order or sorted order algorithms. This is actually the second part of implementing the inorder traversal of a binary tree in Java, in the first part, I have shown you how to solve this problem using recursion and in this part, we'll implement the inorder traversal algorithm without recursion. Now, some of you might argue, why use iteration if the recursive solution is so easy to implement? Well, that's true, but the iterative solution is often regarded better as they are not prone to StackOverFlowError. Another reason why we are discussing the iterative solution here is because of technical interviews.

How to find Kth Smallest Element in a Binary Search Tree? [Solved]

Hello guys, I have been sharing binary search tree-based coding interview questions for quite some time. In the last article, we looked at how to find the maximum sum level in a given binary tree, and in this article, we will find the kth smallest number in a given binary tree like the 5th smallest or 3rd smallest number. Before we find the kth smallest in a Binary search tree, We need to understand the binary search tree. A Binary tree is a data structure in which each node can have at most two children. That is, each node in the binary tree will have data, left child and right child. The first node of the tree is called the Root.

How to find the maximum sum level in binary tree in Java? Example Tutorial

Hello guys, if you are preparing for a coding interview and wondering how to find the maximum sum level in a given binary tree in Java then you have come to the right place. I have been sharing binary tree coding problems for the last few months and in the past, we have seen questions like finding kth smallest element and finding the lowest common ancestor of a binary tree in Java in this article, you will learn how to find the maximum sum level in a given binary tree of integers or numbers. But, before finding the maximum sum level of a binary tree in java, we need to have a good understanding of a binary search tree. Both theoretical knowledge and how to implement binary search tree in Java is important to solve tree-based coding problems

How to Find Lowest Common Ancestor of a Binary Tree in Java? Example Tutorial

Hello guys, if you are wondering how to find the lowest common ancestor of a binary tree in Java then you are at the right place. Earlier, I have shared 40+ binary tree questions and today I am going to share solution of one of the popular binary tree question here. To find the lowest common ancestor of a binary tree in java requires that we run through a binary search tree and how it operates.  What then is a binary search tree? A Binary tree is a data structure in which each node can have at most two children. That is, each node in the binary tree will have data, left child and right child. The first node of the tree is called the Root.

How to check if a node exists in a binary tree or not in Java? Example Tutorial

Hello guys, if you are wondering how to check if a given node exists in a given binary tree or not then you have come to the right place. I have been sharing a lot of binary tree-based programming interview questions in the past few articles. Earlier, we have solved how to find the maximum sum level in a given binary tree, how to find the lowest common ancestor, and how to find Kth smallest element, and in his article, we will solve another classical binary tree problem of search nodes.  Bug, before finding if a node exists in a binary tree or not. It is good to understand what is a  binary tree and binary search tree and how to solve a binary tree-based coding problem. 

How to Print all leaf Nodes of a Binary tree in Java [ Coding Interview Questions]

This is another interesting coding problem that is based on a binary tree and mostly asked beginner programmers. If you have some experience in solving binary tree-based problems then it's rather easy to solve because, like many other binary tree algorithms, you can use recursion to print all leaf nodes of a binary tree in Java. Since the tree is a recursive data structure, you can apply the same algorithm to both the left and right subtree. In order to solve this problem, the first thing you should know is what is a leaf node because if you don't know that then you won't be able to solve the problem. Well, a leaf node is the one whose left and right child nodes are null.

Top 40 Binary Tree Coding Interview Questions for Programmers

Hello guys, if you are preparing for coding interviews and want to master tree-based questions then you have come to the right place. A tree is one of the most important data structures because it allows you to store hierarchical data as opposed to an array and linked list which allows storing linear data. That's why knowledge of tree data structure is very important from an interview point of view. A lot of programmers and my readers have been asking me to share some binary tree-based coding interview questions, just like I have done for the array, linked list, string, software design, patterns, hash table, and data structure in general. 

How to Find/Print Leaf nodes in a Binary Tree in Java without Recursion - Example

In the last article, you have learned how to print all leaf nodes of a binary tree in Java by using Recursion, a useful technique to solve binary tree problems and in this article, we'll answer the same question without using Recursion. Why should we do this? Well, it's a typical pattern on a programming job interview to solve the same problem using both Recursion and Iteration. Since some questions are easy to solve using recursion like linked list problems, binary tree-based problems, tower of Hanoi, or Fibonacci series but their non-recursive solution is comparatively tricky, the interviewer tests candidates against this shift in the algorithm.

Difference between Binary Tree, Binary Search Tree (BST), and Balanced Tree (AVL and Red Black Tree)?

Hello guys, if you are preparing for technical interview for Software Development job then you must prepare well for Data Structure and Algorithms. It is often the difference between selection and non-selection and when it comes to Data Structure, binary search tree is one of the tough topic to master. In the past, I have shared 100+ data structure questions and 40+ binary tree questions and today, I am going to share one of the popular theory or concept questions related to binary tree data structure.  The Tree data structure is one of the essential data structures, but unfortunately, many programmers don't pay enough attention to learning Trees, particularly advanced tree data structures like balanced trees like AVL and Red-Black tree. 

Post order traversal Algorithms for Binary Tree in Java with example

In the last couple of articles, you have learned about pre-order and in-order tree traversal algorithms in Java, and today, you will learn about the post-order traversal in a binary tree. It is the toughest of all three tree traversal algorithms and programmers generally struggle to implement this when asked in a coding interview. Hence it makes sense to understand and practice this algorithm before going for the interview. The post order traversal is also a depth-first algorithm because you go deep before you visit other nodes on the same level.

How to implement PreOrder traversal of Binary Tree in Java - Example Tutorial

The easiest way to implement the preOrder traversal of a binary tree in Java is by using recursion. The recursive solution is hardly 3 to 4 lines of code and exactly mimic the steps, but before that, let's revise some basics about a binary tree and preorder traversal. Unlike array and linked list which have just one way to traverse, I mean linearly, the binary tree has several ways to traverse all nodes because of its hierarchical nature like level order, preorder, postorder, and in order. Tree traversal algorithms are mainly divided into two categories, the depth-first algorithms, and breadth-first algorithms. In depth-first, you go deeper into a tree before visiting the sibling node, for example, you go deep following the left node before you come back and traverse the right node.

Post Order Binary Tree Traversal in Java Without Recursion - Example Tutorial

In the last article, I have shown you how to implement post-order traversal in a binary tree using recursion, and today I am going to teach you about post order traversal without recursion. To be honest, the iterative algorithm of post-order traversal is the toughest among the iterative pre-order and in-order traversal algorithm. The process of post-order traversal remains the same but the algorithm to achieve that effect is different. Since post-order traversal is a depth-first algorithm, you have to go deep before you go wide. I mean, the left subtree is visited first, followed by the right subtree, and finally, the value of a node is printed.

How to Perform Binary Tree InOrder traversal in Java using Recursion? Example Tutorial

The InOrder traversal is one of the three popular ways to traverse a binary tree data structure, the other two being the preOrder and postOrder. During the in-order traversal algorithm, the left subtree is explored first, followed by root, and finally nodes on the right subtree. You start traversal from root then go to the left node, then again go to the left node until you reach a leaf node. At that point in time, you print the value of the node or mark it visited and move to the right subtree. Continuing the same algorithm until all nodes of the binary tree are visited. The InOrder traversal is also known as the left-node-right or left-root-right traversal or LNR traversal algorithm.

How to implement Binary Tree PreOrder Traversal in Java without Recursion? [Solved] Example Tutorial

This is my second article on how to implement binary tree pre-order traversal in Java. In the first part, I have shown how to traverse a binary tree with a pre-order traversal algorithm using Recursion, and in this article, you will learn how to implement pre-order traversal without using Recursion. You might be thinking that why do you need to learn the iterative solution if a recursive solution is possible and easy to write? Well, this type of question is mostly asked in Programming Job interviews and Interviewers like to see how comfortable a candidate is with both Recursion as well as using other data structures and iteration.