Skip to content

Commit 35bedec

Browse files
committed
Tree
1 parent 013f6ce commit 35bedec

13 files changed

+733
-50
lines changed

101.IsSymmetric.cs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// 101. Symmetric Tree
2+
3+
// Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
4+
5+
6+
7+
// Example 1:
8+
9+
10+
// Input: root = [1,2,2,3,4,4,3]
11+
// Output: true
12+
// Example 2:
13+
14+
15+
// Input: root = [1,2,2,null,3,null,3]
16+
// Output: false
17+
18+
/**
19+
* Definition for a binary tree node.
20+
* public class TreeNode {
21+
* public int val;
22+
* public TreeNode left;
23+
* public TreeNode right;
24+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
25+
* this.val = val;
26+
* this.left = left;
27+
* this.right = right;
28+
* }
29+
* }
30+
*/
31+
public class Solution {
32+
public bool IsSymmetric(TreeNode root)
33+
{
34+
if (root == null)
35+
return true;
36+
return CheckInvertTree(root.left, root.right);
37+
}
38+
39+
public bool CheckInvertTree(TreeNode root1, TreeNode root2)
40+
{
41+
if (root1 == null && root2 == null )
42+
return true;
43+
if (root1 == null || root2 == null)
44+
return false;
45+
if (root1.val != root2.val)
46+
return false;
47+
return CheckInvertTree(root1.left, root2.right) &&
48+
CheckInvertTree(root1.right, root2.left);
49+
}
50+
}

102.LevelOrder.cs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// 102. Binary Tree Level Order Traversal
2+
3+
// Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).
4+
5+
6+
7+
// Example 1:
8+
9+
10+
// Input: root = [3,9,20,null,null,15,7]
11+
// Output: [[3],[9,20],[15,7]]
12+
// Example 2:
13+
14+
// Input: root = [1]
15+
// Output: [[1]]
16+
// Example 3:
17+
18+
// Input: root = []
19+
// Output: []
20+
21+
/**
22+
* Definition for a binary tree node.
23+
* public class TreeNode {
24+
* public int val;
25+
* public TreeNode left;
26+
* public TreeNode right;
27+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
28+
* this.val = val;
29+
* this.left = left;
30+
* this.right = right;
31+
* }
32+
* }
33+
*/
34+
public class Solution {
35+
public IList<IList<int>> LevelOrder(TreeNode root) {
36+
IList<IList<int>> res = new List<IList<int>>();
37+
if (root == null)
38+
{
39+
return res;
40+
}
41+
42+
Queue<TreeNode> queue = new Queue<TreeNode>();
43+
queue.Enqueue(root);
44+
while (queue.Count > 0)
45+
{
46+
IList<int> level = new List<int>();
47+
int size = queue.Count;
48+
for (int i = 0; i < size; i++)
49+
{
50+
TreeNode currNode = queue.Dequeue();
51+
level.Add(currNode.val);
52+
if (currNode.right != null)
53+
queue.Enqueue(currNode.right);
54+
if (currNode.left != null)
55+
queue.Enqueue(currNode.left);
56+
}
57+
res.Add(level);
58+
}
59+
return res.Reverse();
60+
}
61+
}

103.ZigzagLevelOrder.cs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// 103. Binary Tree Zigzag Level Order Traversal
2+
// Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).
3+
4+
5+
6+
// Example 1:
7+
8+
9+
// Input: root = [3,9,20,null,null,15,7]
10+
// Output: [[3],[20,9],[15,7]]
11+
// Example 2:
12+
13+
// Input: root = [1]
14+
// Output: [[1]]
15+
// Example 3:
16+
17+
// Input: root = []
18+
// Output: []
19+
20+
/**
21+
* Definition for a binary tree node.
22+
* public class TreeNode {
23+
* public int val;
24+
* public TreeNode left;
25+
* public TreeNode right;
26+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
27+
* this.val = val;
28+
* this.left = left;
29+
* this.right = right;
30+
* }
31+
* }
32+
*/
33+
public class Solution {
34+
public IList<IList<int>> ZigzagLevelOrder(TreeNode root) {
35+
IList<IList<int>> result = new List<IList<int>>();
36+
if (root == null)
37+
return result;
38+
Queue<TreeNode> q = new Queue<TreeNode>();
39+
q.Enqueue(root);
40+
int level = 0;
41+
while (q.Count > 0)
42+
{
43+
int size = q.Count;
44+
List<int> valLevel = new List<int>();
45+
for(int i=0; i< size; i++)
46+
{
47+
TreeNode top = q.Dequeue();
48+
valLevel.Add(top.val);
49+
if(top.left!=null)
50+
q.Enqueue(top.left);
51+
if(top.right!=null)
52+
q.Enqueue(top.right);
53+
}
54+
if (level % 2 == 0)
55+
{
56+
result.Add(valLevel);
57+
}
58+
else
59+
{
60+
List<int> reverseLevelValue = new List<int>();
61+
for(int i = valLevel.Count -1; i >= 0; i--)
62+
{
63+
reverseLevelValue.Add(valLevel[i]);
64+
}
65+
result.Add(reverseLevelValue);
66+
}
67+
level++;
68+
}
69+
return result;
70+
}
71+
}

104.MaxDepth.cs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// 104. Maximum Depth of Binary Tree
2+
3+
// Given the root of a binary tree, return its maximum depth.
4+
5+
// A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
6+
7+
8+
9+
// Example 1:
10+
11+
12+
// Input: root = [3,9,20,null,null,15,7]
13+
// Output: 3
14+
// Example 2:
15+
16+
// Input: root = [1,null,2]
17+
// Output: 2
18+
19+
/**
20+
* Definition for a binary tree node.
21+
* public class TreeNode {
22+
* public int val;
23+
* public TreeNode left;
24+
* public TreeNode right;
25+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
26+
* this.val = val;
27+
* this.left = left;
28+
* this.right = right;
29+
* }
30+
* }
31+
*/
32+
public class Solution {
33+
public int MaxDepth(TreeNode root) {
34+
if (root == null)
35+
return 0;
36+
int leftHeight, rightHeight;
37+
leftHeight = rightHeight = 0;
38+
if (root.left != null)
39+
{
40+
leftHeight = MaxDepth(root.left);
41+
}
42+
if (root.right != null)
43+
{
44+
rightHeight = MaxDepth(root.right);
45+
}
46+
return 1 + Math.Max(leftHeight, rightHeight);
47+
}
48+
}

107.LevelOrderBottom.cs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// 107. Binary Tree Level Order Traversal II
2+
3+
// Given the root of a binary tree, return the bottom-up level order traversal of its nodes' values. (i.e., from left to right, level by level from leaf to root).
4+
5+
6+
7+
// Example 1:
8+
9+
10+
// Input: root = [3,9,20,null,null,15,7]
11+
// Output: [[15,7],[9,20],[3]]
12+
// Example 2:
13+
14+
// Input: root = [1]
15+
// Output: [[1]]
16+
// Example 3:
17+
18+
// Input: root = []
19+
// Output: []
20+
21+
/**
22+
* Definition for a binary tree node.
23+
* public class TreeNode {
24+
* public int val;
25+
* public TreeNode left;
26+
* public TreeNode right;
27+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
28+
* this.val = val;
29+
* this.left = left;
30+
* this.right = right;
31+
* }
32+
* }
33+
*/
34+
public class Solution {
35+
public IList<IList<int>> LevelOrderBottom(TreeNode root) {
36+
IList<IList<int>> res = new List<IList<int>>();
37+
if (root == null)
38+
{
39+
return res;
40+
}
41+
42+
Queue<TreeNode> queue = new Queue<TreeNode>();
43+
queue.Enqueue(root);
44+
while (queue.Count > 0)
45+
{
46+
IList<int> level = new List<int>();
47+
int size = queue.Count;
48+
for (int i = 0; i < size; i++)
49+
{
50+
TreeNode currNode = queue.Dequeue();
51+
level.Add(currNode.val);
52+
if (currNode.left != null)
53+
queue.Enqueue(currNode.left);
54+
if (currNode.right != null)
55+
queue.Enqueue(currNode.right);
56+
}
57+
res.Add(level);
58+
}
59+
IList<IList<int>> finalres = new List<IList<int>>();
60+
for(int i = res.Count-1; i >=0; i--)
61+
{
62+
finalres.Add(res[i]);
63+
}
64+
return finalres;
65+
}
66+
}

110.IsBalanced.cs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// 110. Balanced Binary Tree
2+
3+
// Given a binary tree, determine if it is height-balanced.
4+
5+
// For this problem, a height-balanced binary tree is defined as:
6+
7+
// a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
8+
9+
10+
11+
// Example 1:
12+
13+
14+
// Input: root = [3,9,20,null,null,15,7]
15+
// Output: true
16+
// Example 2:
17+
18+
19+
// Input: root = [1,2,2,3,3,null,null,4,4]
20+
// Output: false
21+
// Example 3:
22+
23+
// Input: root = []
24+
// Output: true
25+
26+
/**
27+
* Definition for a binary tree node.
28+
* public class TreeNode {
29+
* public int val;
30+
* public TreeNode left;
31+
* public TreeNode right;
32+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
33+
* this.val = val;
34+
* this.left = left;
35+
* this.right = right;
36+
* }
37+
* }
38+
*/
39+
public class Solution {
40+
bool isBalanced = true;
41+
public int Height(TreeNode root)
42+
{
43+
if (root == null)
44+
return 0;
45+
int leftHeight = Height(root.left);
46+
int rightHeight = Height(root.right);
47+
48+
if (Math.Abs(leftHeight - rightHeight) > 1)
49+
isBalanced = false;
50+
return Math.Max(leftHeight, rightHeight) + 1;
51+
}
52+
public bool IsBalanced(TreeNode root) {
53+
if (root == null)
54+
return true;
55+
Height(root);
56+
return isBalanced;
57+
}
58+
}

0 commit comments

Comments
 (0)