
g0201_0300.s0226_invert_binary_tree.Solution.swift Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #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_07_01_Time_0_ms_(100.00%)_Space_15.8_MB_(77.75%)
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
func invertTree(_ root: TreeNode?) -> TreeNode? {
if root?.left != nil || root?.right != nil {
let leftTree = root?.left
let rightTree = root?.right
root?.left = rightTree
root?.right = leftTree
invertTree(root?.left)
invertTree(root?.right)
}
return root
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy