Skip to content

Commit ef417c2

Browse files
committed
Modified
1 parent 840963a commit ef417c2

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/main/scala/com/leetcode/scala/monthly2020/july/IslandPerimeter.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ import scala.collection.mutable
2828
/*
2929
* Approach: Breadth First Search (BFS)
3030
* 1. We have defined a case class to store the indexes as pair.
31-
* 2. getIslandStart will find the edge of the island will give the Pair - from which we have to start the BFS.
32-
* 3. getValue will return the value of the Pair.
31+
* 2. getIslandStart will find the edge of the island and return a Pair - from which we need to start our BFS.
32+
* 3. getValue will return the value of a given Pair.
3333
* 4. In Our function islandPerimeter:
3434
* a. First, we will check if the grid is empty. If empty we will return 0.
3535
* b. If grid non-empty we will use getIslandStart to get the Pair from which we need to start.
3636
* c. We will build Queue(To store the nodes to be visited) and HashSet(To store the visited nodes).
3737
* d. We will enqueue the queue with start Pair and we will add the start Pair to the HashSet.
38-
* e. We will dequeue the queue and we will check for the conditions if the node is at the edge of the grid we will add (1 * no_of_edges shared with the edge of the grid) to the perimeter based on the fact that how many edges are at the edge of the grid for that particular node.
39-
* f. We will check if the node share it's edges with any nodes in top, left, right or bottom. If the node shares it's edge with any of the adjacent node and the value of the adjacent node is 1 and the node is not visited then we will enqueue the adjacent node to the queue and we will add the adjacent node to the HashSet.
40-
* g. If the above condition doesn't match then the adjacent node should be 0. So, we will add 1 to the perimeter.
41-
* h. Finally, the queue will be empty and we will be return the perimeter value.
38+
* e. We will dequeue the queue and we will check for the conditions. If the node is at the edge of the grid we will add (1 * no_of_edges shared with the edge of the grid) to the perimeter based on the fact that how many edges are at the edge of the grid for that particular node.
39+
* f. We will check if the node share it's edges with any nodes in top, left, right or bottom. If the node shares it's edge with any of the adjacent node and if the value of the adjacent node is 1 and if the node is not visited then we will enqueue the adjacent node to the queue and we will add the adjacent node to the HashSet.
40+
* g. If above conditions doesn't match then the adjacent node should be 0. So, we will add 1 to the perimeter.
41+
* h. Finally, the queue will be empty and we will return the perimeter value.
4242
*
4343
* */
4444

src/main/scala/com/leetcode/scala/monthly2020/june/TreeNode.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,17 @@ class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null)
2222
"[" + result + "]"
2323
}
2424
}
25+
26+
object TreeNode {
27+
def heightOfTree(node: TreeNode): Int = {
28+
if (node == null) return -1
29+
else return heightOfTree(node.left) + 1 max heightOfTree(node.right) + 1
30+
}
31+
32+
def countNodes(node: TreeNode): Int = {
33+
if (node == null) return 0
34+
else return 1 + countNodes(node.left) + countNodes(node.right)
35+
}
36+
37+
def apply(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null): TreeNode = new TreeNode(_value, _left, _right)
38+
}

0 commit comments

Comments
 (0)