Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spiral Matrix using cpp #282

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions C++/SpiralMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
using namespace std;

int main() {
int rows, cols;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;

int matrix[100][100];

// Taking user input for the matrix
cout << "Enter the matrix elements:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix[i][j];
}
}

// Spiral order printing without using a function
int top = 0, bottom = rows - 1, left = 0, right = cols - 1;

cout << "The spiral order is:\n";
while (top <= bottom && left <= right) {
// Print the top row
for (int i = left; i <= right; i++) {
cout << matrix[top][i] << " ";
}
top++;

// Print the right column
for (int i = top; i <= bottom; i++) {
cout << matrix[i][right] << " ";
}
right--;

// Print the bottom row, if any
if (top <= bottom) {
for (int i = right; i >= left; i--) {
cout << matrix[bottom][i] << " ";
}
bottom--;
}

// Print the left column, if any
if (left <= right) {
for (int i = bottom; i >= top; i--) {
cout << matrix[i][left] << " ";
}
left++;
}
}

return 0;
}
Binary file added C++/SpiralMatrix.exe
Binary file not shown.
84 changes: 84 additions & 0 deletions Java/BinaryTreeVerticalOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import java.util.*;

class TreeNode {
int val;
TreeNode left;
TreeNode right;

TreeNode(int val) {
this.val = val;
left = null;
right = null;
}
}

public class BinaryTreeVerticalOrder {
class NodeColumnPair {
TreeNode node;
int column;

NodeColumnPair(TreeNode node, int column) {
this.node = node;
this.column = column;
}
}

public List<List<Integer>> verticalOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if (root == null) {
return result;
}

Map<Integer, List<Integer>> columnMap = new HashMap<>();
Queue<NodeColumnPair> queue = new LinkedList<>();
int minColumn = 0, maxColumn = 0;
queue.offer(new NodeColumnPair(root, 0));

while (!queue.isEmpty()) {
NodeColumnPair pair = queue.poll();
TreeNode node = pair.node;
int column = pair.column;

columnMap.putIfAbsent(column, new ArrayList<>());
columnMap.get(column).add(node.val);

minColumn = Math.min(minColumn, column);
maxColumn = Math.max(maxColumn, column);


if (node.left != null) {
queue.offer(new NodeColumnPair(node.left, column - 1));
}

if (node.right != null) {
queue.offer(new NodeColumnPair(node.right, column + 1));
}
}


for (int i = minColumn; i <= maxColumn; i++) {
result.add(columnMap.get(i));
}

return result;
}

public static void main(String[] args) {
BinaryTreeVerticalOrder solution = new BinaryTreeVerticalOrder();


TreeNode root = new TreeNode(3);
root.left = new TreeNode(9);
root.right = new TreeNode(20);
root.right.left = new TreeNode(15);
root.right.right = new TreeNode(7);


List<List<Integer>> verticalOrderTraversal = solution.verticalOrder(root);


for (List<Integer> column : verticalOrderTraversal) {
System.out.println(column);
}
}
}