Tree Traversals
Pre Order
1 | const PreOrder = (node) => { |
In Order
1 | const InOrder = (node) => { |
Post Order
1 | const PostOrder = (node) => { |
Level Orider
1 | function levelOrder(root: TreeNode | null): number[][] { |
Complete Binary Tree
Give an array and return a tree construction.
1 |
Maximum Depth of Binary Tree
We go deep inside it’s left and it’s right, compare both of them. The one has greatest value will be add to one.
1 | function maxDepth(root: TreeNode | null): number { |
Minimum Depth of Binary Tree
1 | function minDepth(root: TreeNode | null): number { |
Balanced Binary Tree
A balanced binary tree, also referred to as a height-balanced binary tree, is defined as a binary tree in which the height of the left and right subtree of any node differ by not more than 1.
1 | function isBalanced(root: TreeNode | null): boolean { |
Check If Two Binary Trees Are Equal
Given the roots of two binary trees p and q, write a function to check if they are the same or not.
We can traverse both of them at same time and find if they are equal in the same node.
1 | const isSameNode = (p: TreeNode | null, q: TreeNode | null) => { |
