Skip to content

Commit

Permalink
226. Invert Binary Tree Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
dipti95 committed Jul 8, 2022
1 parent 94e2c64 commit 25b2b75
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions typescript/226-Invert-Binary-Tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function invertTree(root: TreeNode | null): TreeNode | null {
if (!root) return root
const temp: TreeNode | null = root.left
root.left = root.right
root.right = temp

invertTree(root.left)
invertTree(root.right)
return root
}

0 comments on commit 25b2b75

Please sign in to comment.