Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/872.html
766. Toeplitz Matrix (Easy)
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element.
Now given an M x N
matrix, return True
if and only if the matrix is Toeplitz.
Example 1:
Input: matrix = [ [1,2,3,4], [5,1,2,3], [9,5,1,2] ] Output: True Explanation: In the above grid, the diagonals are: "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]". In each diagonal all elements are the same, so the answer is True.
Example 2:
Input: matrix = [ [1,2], [2,2] ] Output: False Explanation: The diagonal "[1, 2]" has different elements.
Note:
matrix
will be a 2D array of integers.matrix
will have a number of rows and columns in range[1, 20]
.matrix[i][j]
will be integers in range[0, 99]
.
Follow up:
- What if the matrix is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?
- What if the matrix is so large that you can only load up a partial row into the memory at once?
Solution 1.
-
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public boolean leafSimilar(TreeNode root1, TreeNode root2) { if (root1 == null && root2 == null) return true; else if (root1 == null || root2 == null) return false; List<Integer> leafValueSequence1 = leafValueSequence(root1); List<Integer> leafValueSequence2 = leafValueSequence(root2); if (leafValueSequence1.size() != leafValueSequence2.size()) return false; int size = leafValueSequence1.size(); for (int i = 0; i < size; i++) { if (leafValueSequence1.get(i) != leafValueSequence2.get(i)) return false; } return true; } public List<Integer> leafValueSequence(TreeNode root) { List<Integer> leafValueSequence = new ArrayList<Integer>(); Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.offer(root); boolean flag = true; while (!queue.isEmpty()) { int size = queue.size(); if (!flag) { for (int i = 0; i < size; i++) { TreeNode node = queue.poll(); leafValueSequence.add(node.val); } } else { flag = false; for (int i = 0; i < size; i++) { TreeNode node = queue.poll(); TreeNode left = node.left, right = node.right; if (left == null && right == null) queue.offer(node); else { flag = true; if (left != null) queue.offer(left); if (right != null) queue.offer(right); } } } } return leafValueSequence; } } ############ /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean leafSimilar(TreeNode root1, TreeNode root2) { List<Integer> l1 = dfs(root1); List<Integer> l2 = dfs(root2); return l1.equals(l2); } private List<Integer> dfs(TreeNode root) { if (root == null) { return new ArrayList<>(); } List<Integer> ans = dfs(root.left); ans.addAll(dfs(root.right)); if (ans.isEmpty()) { ans.add(root.val); } return ans; } }
-
// OJ: https://leetcode.com/problems/toeplitz-matrix // Time: O(MN) // Space: O(1) class Solution { public: bool isToeplitzMatrix(vector<vector<int>>& matrix) { int M = matrix.size(), N = matrix[0].size(); for (int i = 0; i < M; ++i) { for (int x = i + 1, y = 1; x < M && y < N; ++x, ++y) { if (matrix[x][y] != matrix[x - 1][y - 1]) return false; } } for (int i = 1; i < N; ++i) { for (int x = 1, y = i + 1; x < M && y < N; ++x, ++y) { if (matrix[x][y] != matrix[x - 1][y - 1]) return false; } } return true; } };
-
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool: def dfs(root): if root is None: return [] ans = dfs(root.left) + dfs(root.right) return ans or [root.val] return dfs(root1) == dfs(root2) ############ # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def leafSimilar(self, root1, root2): """ :type root1: TreeNode :type root2: TreeNode :rtype: bool """ leaves1 = [] leaves2 = [] self.inOrder(root1, leaves1) self.inOrder(root2, leaves2) return leaves1 == leaves2 def inOrder(self, root, leaves): if not root: return self.inOrder(root.left, leaves) if not root.left and not root.right: leaves.append(root.val) self.inOrder(root.right, leaves)
-
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func leafSimilar(root1 *TreeNode, root2 *TreeNode) bool { var dfs func(*TreeNode) []int dfs = func(root *TreeNode) []int { if root == nil { return []int{} } ans := dfs(root.Left) ans = append(ans, dfs(root.Right)...) if len(ans) == 0 { ans = append(ans, root.Val) } return ans } return reflect.DeepEqual(dfs(root1), dfs(root2)) }