General
104 Max depth
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;111 Min depth
public int minDepth(TreeNode root) {
if(root == null) return 0;
if(root.left == null) return minDepth(root.right) + 1;
if(root.right == null) return minDepth(root.left) + 1;
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
}110 Check Balanced
public boolean isBalanced(TreeNode root) {
return maxDepth(root) == -1 ? false : true;
}
private int maxDepth(TreeNode root){
if(root == null) return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
if(left == -1 || right == -1 || Math.abs(left-right) > 1) return -1;
return Math.max(left, right) + 1;
}285 Inorder Successor/Predecessor in BST

654 Maximum Binary Tree
222 Count Complete TreeNode
863. All Nodes Distance K in Binary Tree

1145. Binary Tree Coloring Game
Last updated