Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.shekhargulati.ninetynine_problems._04_binary_trees;

/**
* (*) Check whether a given term represents a binary tree
*/
public class P50 {

/**
* Common Tree Class
*
* implements {@link Comparable}
* @param <T> extends {@link Comparable } the id for each node
*/
public static class Node<T extends Comparable<T>> implements Comparable<Node<T>> {
T id;
Node<T> left;
Node<T> right;

public Node(T id){
this.id=id;
this.left=null;
this.right=null;
}

/**
* To compare two nodes
*
* @param object {@link Node} the instance of the node class
* @return the positive or negative value after comparing two nodes
* */
@Override
public int compareTo(Node<T> object){
return this.id.compareTo(object.id)>0?1:-1;
}
}

/**
* To see if the input is a binary tree or not
*
* @param node {@link Node}
* @return true or false depending the tree is binary or not
*/
public static boolean isTree(Node node){
if(node==null || (node.left==null && node.left==null)){
return true;
}
if(node.left!=null){
if(node.compareTo(node.left)>0){
if(!isTree(node.left)){
return false;
}
}else {
return false;
}
}
if(node.right!=null){
if(node.compareTo(node.right)<0){
if(!isTree(node.right)){
return false;
}
}else {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.shekhargulati.ninetynine_problems._04_binary_trees;

/**
* (*) Count the leaves of a binary tree
*/
public class P57 {

/**
* Common Tree Class
*
* implements {@link Comparable}
* @param <T> extends {@link Comparable } the id for each node
*/
public static class Node<T extends Comparable<T>> implements Comparable<Node<T>>{

T id;
Node<T> left;
Node<T> right;

public Node(T id){
this.id=id;
}

/**
* To compare two nodes
*
* @param node {@link Node} the instance of the node class
* @return the positive or negative value after comparing two nodes
*/
@Override
public int compareTo(Node<T> node){
if(this.id.compareTo(node.id)==0) return 0;
if(this.id.compareTo(node.id)>0) return 1;
return -1;
}
}
/**
* To count the leaves on a binary tree
*
* @param root {@link Node} the binary tree
* @param N the count of leaves
* @return integer the count of leaves
*/
public static int count_leaves(Node root, int N){
if(root==null) return N;
else if(root.left==null && root.right==null) N++;
else if(root.left != null && root.right==null) N+=count_leaves(root.left,N);
else if(root.left == null && root.right!=null) N+=count_leaves(root.right,N);
else N+=count_leaves(root.left,N)+count_leaves(root.right,N);
return N;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.shekhargulati.ninetynine_problems._04_binary_trees;

import java.util.List;

/**
* (*) Collect the leaves of a binary tree in a list
*/
public class P58 {

/**
* Common Tree Class
*
* implements {@link Comparable}
* @param <T> extends {@link Comparable } the id for each node
*/
public static class Node<T extends Comparable<T>> implements Comparable<Node<T>>{

T id;
Node<T> left;
Node<T> right;

public Node(T id){ this.id=id; }

/**
* To compare two nodes
*
* @param node {@link Node} the instance of the node class
* @return the positive or negative value after comparing two nodes
*/
@Override
public int compareTo(Node<T> node){
if(this.id.compareTo(node.id)==0) return 0;
if(this.id.compareTo(node.id)>0) return 1;
return -1;
}
}
/**
* To list the leaves on a binary tree
*
* @param root {@link Node} the binary tree
* @param S {@link List} the list of leaves
* @return {@link List} the list of leaves
*/
public static List<Node> leaves(Node root, List<Node> S){
if(root==null) return S;
else if(root.left==null && root.right==null) S.add(root);
else if(root.left != null && root.right==null) leaves(root.left,S);
else if(root.left == null && root.right!=null) leaves(root.right,S);
else{
leaves(root.left,S);
leaves(root.right,S);
}
return S;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.shekhargulati.ninetynine_problems._04_binary_trees;

import java.util.List;

/**
* (*) Collect the internal nodes of a binary tree in a list
*/
public class P59 {

/**
* Common Tree Class
*
* implements {@link Comparable}
* @param <T> extends {@link Comparable } the id for each node
*/
public static class Node<T extends Comparable<T>> implements Comparable<Node<T>>{

T id;
Node<T> left;
Node<T> right;

public Node(T id){
this.id=id;
}

/**
* To compare two nodes
*
* @param node {@link Node} the instance of the node class
* @return the positive or negative value after comparing two nodes
*/
@Override
public int compareTo(Node<T> node) {
if (this.id.compareTo(node.id) > 0) return 1;
else if (this.id.compareTo(node.id) == 0) return 0;
else return -1;
}
}

/**
* To collect the internal nodes of a binary tree in a list
*
* @param root {@link Node} the binary tree
* @param S {@link List} list of internal nodes of a binary tree
* @return {@link List} of {@link Node} List of internal nodes of a binary tree
*/
public static List<Node> internals(Node root, List<Node> S){
if(root==null || (root.left==null && root.right==null)) return S;
S.add(root);
if(root.left!=null) S=internals(root.left, S);
if(root.right!=null) S=internals(root.right,S);
return S;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.shekhargulati.ninetynine_problems._04_binary_trees;

import java.util.List;

/**
* (*) Collect the nodes at a given level in a list
*/
public class P60 {

/**
* Common Tree Class
*
* implements {@link Comparable}
* @param <T> extends {@link Comparable } the id for each node
*/
public static class Node<T extends Comparable<T>> implements Comparable<Node<T>>{

T id;
Node<T> left;
Node<T> right;

public Node(T id){
this.id=id;
}

/**
* To compare two nodes
*
* @param node {@link Node} the instance of the node class
* @return the positive or negative value after comparing two nodes
*/
@Override
public int compareTo(Node<T> node){
if(this.id.compareTo(node.id)==0){
return 0;
}else if(this.id.compareTo(node.id)>0){
return 1;
}else{
return -1;
}
}
}

/**
* To collect the nodes at a given level in a list
*
* @param root {@link Node} the binary tree
* @param S {@link List} list of nodes of a binary tree at a specific level
* @param L the level at which we need to collect nodes
* @return {@link List} of {@link Node} list of nodes of a binary tree at a specific level
*/
public static List<Node> atLevel(Node root, List<Node> S, int L){
if(root==null) return S;
if(L==1){
S.add(root);
return S;
}
if(root.left!=null) S=atLevel(root.left, S, L-1);
if(root.right!=null) S=atLevel(root.right, S, L-1);
return S;
}
}