Skip to content

Commit 1c8d799

Browse files
Invert Binary Tree
1 parent e15581f commit 1c8d799

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/main/java/org/example/Tree.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,14 @@ private int sumOfNodes(Node root) {
196196

197197
return root.value + sumOfNodes(root.leftChild) + sumOfNodes(root.rightChild);
198198
}
199+
private void invertBinaryTree(Node root) {
200+
if (root == null) return;
199201

202+
var temp = root.leftChild;
203+
root.leftChild = root.rightChild;
204+
root.rightChild = temp;
205+
206+
invertBinaryTree(root.leftChild);
207+
invertBinaryTree(root.rightChild);
208+
}
200209
}

0 commit comments

Comments
 (0)