Skip to content

Commit 99d4737

Browse files
committed
Added trees problem solutions
1 parent 7228c1d commit 99d4737

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using ConsoleApp1.Helpers;
3+
4+
namespace ConsoleApp1.Trees.Easy;
5+
6+
// 543. Diameter of Binary Tree
7+
public class DiameterOfBinaryTree
8+
{
9+
private int _max = 0;
10+
11+
// Time complexity: O(n); Space complexity: O(n).
12+
public int Diameter(TreeNode root)
13+
{
14+
if (root == null) return 0;
15+
16+
Dfs(root);
17+
18+
return _max;
19+
}
20+
21+
private int Dfs(TreeNode root)
22+
{
23+
if (root == null) return 0;
24+
25+
var left = Dfs(root.left);
26+
var right = Dfs(root.right);
27+
28+
_max = Math.Max(_max, left + right);
29+
30+
return Math.Max(left, right) + 1;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using ConsoleApp1.Helpers;
2+
3+
namespace ConsoleApp1.Trees.Easy;
4+
5+
// 100. Same Tree
6+
public class SameTree
7+
{
8+
// Time complexity: O(n); Space complexity: O(n).
9+
public bool IsSameTree(TreeNode p, TreeNode q)
10+
{
11+
if (p == null && q == null)
12+
return true;
13+
14+
if (p == null || q== null)
15+
return false;
16+
17+
return p.val == q.val &&
18+
IsSameTree(p.left, q.left) &&
19+
IsSameTree(p.right, q.right);
20+
}
21+
}

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
- [Stack](#stack)
1414
- [Binary Search](#binary-search)
1515
- [Linked List](#linked-list)
16-
- [Trees][#trees]
16+
- [Trees](#trees)
1717
- [Hints](#hints)
1818

1919
## Arrays & Hashing
@@ -96,10 +96,14 @@
9696
| Problem | Complexity | Time Complexity | Space Complexity | Solution Hints |
9797
| ------- | ---------- | --------------- | ---------------- | -------------- |
9898
| [94. Binary Tree Inorder Traversal](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/BinaryTreeInorderTraversal.cs) | Easy | O(n) | O(n) | - For iterative approach use stack (while stack.Any || currNode != null) <br /> - If currNode != null - push node to stack, go left <br /> - If currNode == null - get prevNode from stack, add value to result,go the prevNode.right |
99+
100+
| [100. Same Tree](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/SameTree.cs) | Easy | O(n) | O(n) | Check that values of trees nodes are equal, use recursion to repeat for each node (left and right) |
101+
99102
| [104. Maximum Depth of Binary Tree](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/MaximumDepthOfBinaryTree.cs) | Easy | O(n) | O(n) | - DFS iterative - use stack to store nodes and depth <br /> - BFS iterative - use queue to count levels <br /> - Recursion - return Max of recursive calls of left and right children + 1 |
100103
| [144. Binary Tree Preorder Traversal](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/BinaryTreePreorderTraversal.cs) | Easy | O(n) | O(n) | - For iterative approach use stack (while stack.Any || currNode != null) <br /> - If currNode != null - add value to result, push currNode.right to stack, go left <br /> - If currNode == null - currNode = node from stack |
101104
| [145. Binary Tree Postorder Traversal](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/BinaryTreePostorderTraversal.cs) | Easy | O(n) | O(n) | - For iterative approach use stack (while stack.Any) to store node ond visited flag, push rootto stack <br /> - Pop from stack, if visited - add to result, otherwise - push curr as visited, left and right as not visited to stack |
102105
| [226. Invert Binary Tree](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/BinaryTreePostorderTraversal.cs) | Easy | O(n) | O(n) | - Check null, swap left and right children, recursive call for both left and right child |
106+
| [543. Diameter of Binary Tree](AlgorithmsAndDS/AlgorithmsAndDS/Trees/Easy/DiameterOfBinaryTree.cs) | Easy | O(n) | O(n) | - For each node we need to identify max depth of left and right child <br /> - Calculate diameter as maxDepthLeft + maxDepthRight <br /> - Compare diameter with stored max value |
103107

104108
## Hints
105109

0 commit comments

Comments
 (0)