Skip to content

Commit 360fe20

Browse files
committed
feat: invert-binary-tree
1 parent 8b5e043 commit 360fe20

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

invert-binary-tree/minji-go.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/invert-binary-tree/">week10-1. invert-binary-tree </a>
3+
* <li>Description: Design an algorithm to serialize and deserialize a binary tree </li>
4+
* <li>Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree </li>
5+
* <li>Time Complexity: O(N), Runtime 0ms </li>
6+
* <li>Space Complexity: O(N), Memory 41.28MB </li>
7+
*/
8+
class Solution {
9+
public TreeNode invertTree(TreeNode root) {
10+
if (root == null) return null;
11+
12+
Queue<TreeNode> queue = new LinkedList<>();
13+
queue.offer(root);
14+
15+
while (!queue.isEmpty()) {
16+
TreeNode parent = queue.poll();
17+
TreeNode temp = parent.left;
18+
parent.left = parent.right;
19+
parent.right = temp;
20+
if (parent.left != null) queue.offer(parent.left);
21+
if (parent.right != null) queue.offer(parent.right);
22+
}
23+
24+
return root;
25+
}
26+
}

0 commit comments

Comments
 (0)