Methods
(static) isSameTree(tree1, tree2) → {boolean}
Given the roots of two binary trees p and q, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
For example,
Input: p = [1, 2, 3], q = [1, 2, 3]
Output: true
- Source:
- See:
Parameters:
Name | Type | Description |
---|---|---|
tree1 |
AVLTree
|
First AVL tree |
tree2 |
AVLTree
|
Second AVL tree |
Returns:
- Type:
-
boolean
Both trees are same or not
(static) isSubTree(root, subRoot) → {boolean}
Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise.
A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself.
For example,
Input: root = [3, 4, 5, 1, 2], subRoot = [4, 1, 2]
Output: true
- Source:
- See:
Parameters:
Name | Type | Description |
---|---|---|
root |
TreeNode
|
root of AVL tree |
subRoot |
TreeNode
|
subRoot of AVL tree |
Returns:
- Type:
-
boolean
true if subTree is a tree of another tree otherwise false
getDiameter() → {Number}
Given the root of a binary tree, return the length of the diameter of the tree
The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
For example,
Input: [1, 2, 3, 4, 5]
Output: 3
- Source:
- See:
Returns:
- Type:
-
Number
Height of AVL tree
getHeight() → {Number}
Given the root of a binary tree, return its maximum depth/height.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
For example,
Input: [3, 9, 20, null, null, 15, 7]
Output: 3
- Source:
- See:
Returns:
- Type:
-
Number
Diameter of AVL tree
insert(value, current)
Insert the node into the AVL tree at given node. If not provided, root will be consider as current.
Time Complexity: O(logN) in the average case and O(N) in worst case.
- Source:
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value |
value that needs to be inserted |
||
current |
null |
Current Node |
Returns:
void
invert() → {TreeNode}
Given the root of a binary tree, invert the tree, and return its root.
For example,
Input: [4, 2, 7, 1, 3, 6, 9]
Output: [4, 7, 2, 9, 6, 3, 1]
- Source:
- See:
Returns:
- Type:
-
TreeNode
Inverted AVL tree
isBalanced() → {boolean}
Given a binary tree, determine if it is height-balanced.
a height-balanced binary tree is defined as:
"a binary tree in which the left and right subtrees of every node differ in height by no more than 1."
For example,
Input: [3, 9, 20, null, null, 15, 7]
Output: true
- Source:
- See:
Returns:
- Type:
-
boolean
Tree is balanced or not