the Height can be maintain while insertion in BST
(int height) should also be added in tree class where data is declared
void height(){
if(root==null)return -1;
return root.height;
}
The upper function will return -1 if root is null else return current height of root
NOW while inserting into BST just maintain the height of root before returning the root;
root.height= Math.max(height(root.left),height(root.right))+1;
the (+1) in the above code will increase the height by 1 when a new nood gets inserted in BST
bfs dfs is a important searching techniqes of a Tree
In BFS, we perform level wise traversal of node using queue, when the upper level Nodes are traversed then we come to the lower level nodes.
Queue q= new LinkedList<>()
q.add(root)
while(!q.isEmpty){
itr=q.remove();
check if conditions for adding childs in queue
}
DFS is a top to bottom search
PREORDER , POSTORDER , INORDER is used in DFS
INORDER = > Inorder gives soted data of a BST
POSTORDER => postorder can be use when we want to delete tree
the next is also provided hence only use LinkedList Logic to solve this question.
traverse level wise and find the last node of each level, it can be found by using nested for loop inside while loop, inside will run till the complete level of tree and add last value