Last active
September 17, 2023 10:52
-
-
Save rohanjai777/c24d9c8cbee66d26a6dec4b22057841c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Size of binary tree | |
public static int getSize(Node node) | |
{ | |
if(node == null){ | |
return 0; | |
} | |
int left = getSize(node.left); | |
int right = getSize(node.right); | |
return left+right+1; | |
} | |
Height of binary tree | |
int height(Node node) | |
{ | |
if(node == null){ | |
return 0; | |
} | |
int left = height(node.left); | |
int right = height(node.right); | |
return Math.max(left,right)+1; | |
} | |
Sum of all nodes of binary tree | |
static int sumBT(Node node){ | |
if(node == null){ | |
return 0; | |
} | |
int left = sumBT(node.left); | |
int right = sumBT(node.right); | |
return left+right+node.data; | |
} | |
Maximum node in binary tree | |
public static int max(Node node) { | |
if(node == null){ | |
return 0; | |
} | |
int left = max(node.left); | |
int right = max(node.right); | |
return Math.max(node.data, Math.max(left,right)); | |
} | |
Right And Left view of binary tree - Increase level | |
public class Solution { | |
int maxLevel = -1; | |
ArrayList<Integer> result = new ArrayList<>(); | |
public ArrayList<Integer> solve(TreeNode A) { | |
getLevels(A,0); | |
return result; | |
} | |
public void getLevels(TreeNode A, int level){ | |
if(A == null){ | |
return; | |
} | |
if(level > maxLevel){ //Increase the level for right only | |
result.add(A.val); | |
maxLevel = level; | |
} | |
getLevels(A.right, level+1); //For left view - getLevels(A.left,level+1) | |
getLevels(A.left, level+1); //For left view - getLevels(A.right,level+1) | |
} | |
} | |
Minimum depth of tree - Root to leaf min path | |
public class Solution { | |
public int minDepth(TreeNode A) { | |
if(A == null){ | |
return Integer.MAX_VALUE; | |
} else if (A.left == null && A.right == null){ //leaf node | |
return 1; | |
} | |
int left = minDepth(A.left); | |
int right = minDepth(A.right); | |
return Math.min(left,right)+1; //get min of left and right and add 1 for current level | |
} | |
} | |
Target Sum from root to leaf node in binary tree | |
public class Solution { | |
public int hasPathSum(TreeNode A, int B) { | |
return checkNodeWithSum(A,B,0); | |
} | |
public int checkNodeWithSum(TreeNode node, int target, int currsum){ | |
if(node == null){ | |
return 0; | |
}else if(node.left == null && node.right == null){ //leaf node | |
if((currsum+node.val) == target){ | |
return 1; //found the target | |
} | |
return 0; | |
} | |
int leftVal = checkNodeWithSum(node.left,target,currsum+node.val); | |
if(leftVal == 1) return 1; | |
int rightVal = checkNodeWithSum(node.right,target,currsum+node.val); | |
if(rightVal == 1) return 1; | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment