Skip to content

Commit 929d8d9

Browse files
committed
invert binary tree solution
1 parent 139e6c1 commit 929d8d9

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

invert-binary-tree/hyer0705.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function invertTree(root: TreeNode | null): TreeNode | null {
16+
if (!root) return null;
17+
18+
const temp = root.left;
19+
root.left = root.right;
20+
root.right = temp;
21+
22+
invertTree(root.left);
23+
invertTree(root.right);
24+
25+
return root;
26+
}

0 commit comments

Comments
 (0)