# binary trees

## PostOrder Node Iterator of Binary Tree

Introduction This is going to be a short post. The problem statement at hand is to write PostOrder Node Iterator of Binary Tree. For those who do not know the Iterator Design Pattern or Post Order Successor, check the next section. Iterator Design Pattern This pattern is used to iterate through a collection or group. The Java Iterator supports the following three methods : hasNext() : returns a boolean true if the collection has elements after the last returned element, false otherwise. next() : returns the next element in the collection. remove() : removes the element from the coll...

Introduction Problem Statement : Given a regular binary tree with left, right and peer node pointers. The left and the right pointers are already populated. We need to make the peer pointer point to the next right neighbor on the same level. Understanding the problem Here is a diagram to explain what is needed. The red peer pointer points to the immediate right neighbor if the neighbor exists. Approach 1 The first solution which comes in mind is to do a custom, level order traversal as done in the post. And, in each iteration of the inner loop, just link each of the node to the next in ...

## Tree Diameter

Introduction This is the fifth article in the Tree Traversals - Online Classes. A very interesting problem is to find the diameter of a tree. The challenge is not in coding the algorithm but in understanding the meaning of the problem. We will try to define the problem so that visualizing the solution becomes easy. Defining the problem - Tree Diameter Few confusions about the diameter must be avoided before we start. The tree diameter is always the distance between two leaf nodes The diameter may or may not pass through the root. The diameter can also lie on one of the two sides,...