All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0201_0300.s0226_invert_binary_tree.solution.js Maven / Gradle / Ivy

The newest version!
// #Easy #Top_100_Liked_Questions #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
// #Data_Structure_I_Day_12_Tree #Level_2_Day_6_Tree #Udemy_Tree_Stack_Queue
// #Big_O_Time_O(n)_Space_O(n) #2024_12_17_Time_0_ms_(100.00%)_Space_49.4_MB_(64.65%)

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var invertTree = function(root) {
    if (root === null) {
        return null
    }
    let temp = root.left
    root.left = invertTree(root.right)
    root.right = invertTree(temp)
    return root
};

export { invertTree }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy