Skip to content

Commit 39a654b

Browse files
committed
#226: Invert Binary Tree; solution & tests
1 parent 9d73af0 commit 39a654b

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* 226
3+
* Invert Binary Tree
4+
**
5+
* Given the root of a binary tree,
6+
* invert the tree,
7+
* and return its root.
8+
*
9+
* Example 1:
10+
* Input: root = [4,2,7,1,3,6,9]
11+
* Output: [4,7,2,9,6,3,1]
12+
*
13+
* Example 2:
14+
* Input: root = [2,1,3]
15+
* Output: [2,3,1]
16+
*
17+
* Example 3:
18+
* Input: root = []
19+
* Output: []
20+
*
21+
* Constraints:
22+
* • The number of nodes in the tree is in the range [0, 100].
23+
* • -100 <= Node.val <= 100
24+
**
25+
* https://leetcode.com/problems/invert-binary-tree/
26+
***/
27+
28+
/**
29+
* Definition for a binary tree node.
30+
* public class TreeNode {
31+
* public int val;
32+
* public TreeNode left;
33+
* public TreeNode right;
34+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
35+
* this.val = val;
36+
* this.left = left;
37+
* this.right = right;
38+
* }
39+
* }
40+
***/
41+
42+
using Problems.Common;
43+
44+
namespace Problems;
45+
46+
public class InvertBinaryTree
47+
{
48+
public TreeNode InvertTree( TreeNode root )
49+
{
50+
if ( root is null )
51+
{
52+
return root;
53+
}
54+
55+
(root.left, root.right) = (root.right, root.left);
56+
57+
InvertTree( root.left );
58+
InvertTree( root.right );
59+
60+
return root;
61+
}
62+
}

c-sharp/Problems/trees/common/TreeNode.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,61 @@ public static TreeNode CreateBST( int[] values )
7070
return result;
7171
}
7272

73+
// BST: Binary Search Tree
74+
public static TreeNode CreateDescBST( int[] values )
75+
{
76+
if ( values.Length == 0 )
77+
{
78+
return null;
79+
}
80+
81+
TreeNode result = new()
82+
{
83+
val = values[0]
84+
};
85+
86+
for ( int i = 1; i < values.Length; i++ )
87+
{
88+
TreeNode temp = result;
89+
90+
while ( true )
91+
{
92+
if ( values[i] == temp.val )
93+
{
94+
break;
95+
}
96+
else if ( values[i] < temp.val )
97+
{
98+
if ( temp.right is null )
99+
{
100+
temp.right = new TreeNode( values[i] );
101+
102+
break;
103+
}
104+
else
105+
{
106+
temp = temp.right;
107+
}
108+
}
109+
else
110+
{
111+
if ( temp.left is null )
112+
{
113+
temp.left = new TreeNode( values[i] );
114+
115+
break;
116+
}
117+
else
118+
{
119+
temp = temp.left;
120+
}
121+
}
122+
}
123+
}
124+
125+
return result;
126+
}
127+
73128
public override bool Equals( object? obj )
74129
{
75130
return obj is TreeNode treeNode &&
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections;
2+
3+
using NUnit.Framework;
4+
5+
using Problems;
6+
using Problems.Common;
7+
8+
public class InvertBinaryTreeTests
9+
{
10+
[TestCaseSource( nameof( TestCases ) )]
11+
public TreeNode InvertTreeTest( TreeNode root ) =>
12+
new InvertBinaryTree().InvertTree( root );
13+
14+
private static IEnumerable TestCases
15+
{
16+
get
17+
{
18+
yield return new TestCaseData( TreeNode.CreateBST( [4, 2, 7, 1, 3, 6, 9] ) )
19+
.Returns( TreeNode.CreateDescBST( [4, 7, 2, 9, 6, 3, 1] ) );
20+
21+
yield return new TestCaseData( TreeNode.CreateBST( [2, 1, 3] ) )
22+
.Returns( TreeNode.CreateDescBST( [2, 3, 1] ) );
23+
24+
yield return new TestCaseData( TreeNode.CreateBST( [] ) )
25+
.Returns( TreeNode.CreateDescBST( [] ) );
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)