Invert a binary tree
Easytreedfsbfsrecursion
Mirror the tree: swap the left and right child of every node. Return the root.
Examples write a tree level by level, with null for a missing child: [3,9,20,null,null,15,7].
Example 1
Input: root = [4,2,7,1,3,6,9] Output: [4,7,2,9,6,3,1]
Example 2
Input: root = [2,1,3] Output: [2,3,1]
Constraints
- 0 ≤ number of nodes ≤ 100
- -100 ≤ Node.val ≤ 100
Submit checks your solution against 8 tests, including hidden ones.
// Provided by the judge: keep these definitions
class TreeNode {
constructor(val = 0, left = null, right = null) {
this.val = val
this.left = left
this.right = right
}
}
/**
* @param {TreeNode|null} root
* @return {TreeNode|null}
*/
function invertTree(root) {
}