Facebook Pixel

558. Logical OR of Two Binary Grids Represented as Quad-Trees

Problem Description

This problem asks you to perform a logical OR operation on two binary matrices represented as Quad-Trees.

What is a Quad-Tree? A Quad-Tree is a tree data structure where each internal node has exactly four children representing the four quadrants of a grid: topLeft, topRight, bottomLeft, and bottomRight. Each node has two properties:

  • val: True if the node represents a grid of all 1's, False if it represents all 0's
  • isLeaf: True if the node is a leaf (represents a uniform region), False if it has four children

How Quad-Trees represent binary matrices:

  • If a region in the matrix has all the same values (all 0's or all 1's), it's represented as a leaf node with isLeaf = True and val set to that value
  • If a region has mixed values, it's represented as an internal node with isLeaf = False, and the region is divided into four equal quadrants, each represented by a child node

The Task: Given two Quad-Trees (quadTree1 and quadTree2) representing two n × n binary matrices, you need to:

  1. Perform a bitwise OR operation on the corresponding elements of the two matrices
  2. Return a new Quad-Tree representing the resulting matrix

Bitwise OR logic:

  • 0 OR 0 = 0
  • 0 OR 1 = 1
  • 1 OR 0 = 1
  • 1 OR 1 = 1

Key optimization: When performing the OR operation on Quad-Trees:

  • If both nodes are leaves, the result is a leaf with value val1 OR val2
  • If one node is a leaf with value 1 (all 1's), the result for that region is all 1's
  • If one node is a leaf with value 0 (all 0's), the result is the same as the other tree for that region
  • If neither is a leaf, recursively compute OR for all four quadrants and potentially merge them if they all have the same value
Quick Interview Experience
Help others by sharing your interview experience
Have you seen this problem before?

Intuition

The key insight is that we can leverage the hierarchical structure of Quad-Trees to avoid reconstructing the entire matrix. Instead of converting both trees back to matrices, performing OR operation, and then building a new tree, we can work directly with the tree structure.

Think about what the OR operation means for different node combinations:

  1. When both nodes are leaves: This is straightforward - we just OR their values. If one represents all 0's and the other all 1's, the result is all 1's.

  2. When one node is a leaf with value 1: Since 1 OR anything = 1, the entire region becomes all 1's. We can directly return this leaf node without further processing.

  3. When one node is a leaf with value 0: Since 0 OR x = x, the result is identical to whatever the other tree has for that region. We can return the other node directly.

  4. When both nodes are internal nodes: We need to recursively process all four quadrants, as different parts might have different results.

The recursive approach naturally follows the tree structure. At each level, we decide whether we can take a shortcut (cases 1-3) or need to dive deeper (case 4).

After processing all four children of an internal node, there's an optimization opportunity: if all four resulting quadrants are leaves with the same value, we can merge them into a single leaf node. This compression step ensures our result tree is as compact as possible, avoiding unnecessary subdivisions where the entire region is uniform.

This approach is efficient because:

  • We only traverse parts of the tree that need processing
  • We reuse existing nodes when possible (when one tree has all 0's)
  • We perform the OR operation implicitly through the tree structure rather than on individual matrix elements

Learn more about Tree and Divide and Conquer patterns.

Solution Approach

The solution uses a recursive depth-first search (DFS) approach to traverse both Quad-Trees simultaneously and construct the result tree.

Main Algorithm Structure:

The solution defines a helper function dfs(t1, t2) that processes two nodes from the input trees and returns the corresponding node for the result tree.

Case-by-case handling:

  1. Both nodes are leaves: if t1.isLeaf and t2.isLeaf:

    • Create a new leaf node with value t1.val or t2.val
    • This directly computes the OR of two uniform regions
  2. First node is a leaf: if t1.isLeaf:

    • If t1.val is True (all 1's), return t1 since 1 OR anything = 1
    • If t1.val is False (all 0's), return t2 since 0 OR x = x
  3. Second node is a leaf: if t2.isLeaf:

    • If t2.val is True (all 1's), return t2 since anything OR 1 = 1
    • If t2.val is False (all 0's), return t1 since x OR 0 = x
  4. Both nodes are internal nodes:

    • Create a new internal node res
    • Recursively process all four quadrants:
      res.topLeft = dfs(t1.topLeft, t2.topLeft)
      res.topRight = dfs(t1.topRight, t2.topRight)
      res.bottomLeft = dfs(t1.bottomLeft, t2.bottomLeft)
      res.bottomRight = dfs(t1.bottomRight, t2.bottomRight)

Optimization - Node Merging:

After processing the four children, the algorithm checks if they can be merged:

  • Check if all four children are leaves: isLeaf = (res.topLeft.isLeaf and res.topRight.isLeaf and ...)
  • Check if all four children have the same value: sameVal = (res.topLeft.val == res.topRight.val == ...)
  • If both conditions are true, replace the internal node with one of its children (they're all identical): res = res.topLeft

This merging step ensures the result tree is optimally compressed, avoiding unnecessary subdivisions for uniform regions.

Time Complexity: O(n) where n is the total number of nodes in both input trees combined (worst case when we need to visit all nodes).

Space Complexity: O(h) where h is the height of the tree, due to the recursion stack. In the worst case, h can be O(log m) where m is the dimension of the matrix.

Ready to land your dream job?

Unlock your dream job with a 3-minute evaluator for a personalized learning plan!

Start Evaluator

Example Walkthrough

Let's walk through a small example with two 4×4 binary matrices and their Quad-Tree representations.

Matrix 1:

1 1 0 0
1 1 0 0
1 1 1 1
1 1 1 1

Matrix 2:

1 1 1 1
1 1 1 1
0 0 1 1
0 0 1 1

Quad-Tree representations:

Tree 1:

Root (internal)
├─ topLeft: Leaf(val=1)      # All 1's
├─ topRight: Leaf(val=0)     # All 0's
├─ bottomLeft: Leaf(val=1)   # All 1's
└─ bottomRight: Leaf(val=1)  # All 1's

Tree 2:

Root (internal)
├─ topLeft: Leaf(val=1)      # All 1's
├─ topRight: Leaf(val=1)     # All 1's
├─ bottomLeft: Leaf(val=0)   # All 0's
└─ bottomRight: Leaf(val=1)  # All 1's

Step-by-step OR operation:

  1. Start at roots: Both are internal nodes, so we recursively process all four quadrants.

  2. Process topLeft quadrant:

    • Tree1: Leaf(val=1), Tree2: Leaf(val=1)
    • Both are leaves → Result: Leaf(val=1 OR 1 = 1)
  3. Process topRight quadrant:

    • Tree1: Leaf(val=0), Tree2: Leaf(val=1)
    • Both are leaves → Result: Leaf(val=0 OR 1 = 1)
  4. Process bottomLeft quadrant:

    • Tree1: Leaf(val=1), Tree2: Leaf(val=0)
    • Both are leaves → Result: Leaf(val=1 OR 0 = 1)
  5. Process bottomRight quadrant:

    • Tree1: Leaf(val=1), Tree2: Leaf(val=1)
    • Both are leaves → Result: Leaf(val=1 OR 1 = 1)
  6. Check for merging:

    • All four resulting children are leaves with val=1
    • Can merge into a single leaf node

Final Result: Single Leaf(val=1) representing the entire 4×4 matrix of all 1's

Resulting Matrix:

1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

This example demonstrates how the algorithm:

  • Recursively processes matching quadrants from both trees
  • Performs OR operations on leaf nodes
  • Optimizes by merging uniform regions into single leaf nodes
  • Leverages the tree structure to avoid processing individual matrix elements

Solution Implementation

1"""
2# Definition for a QuadTree node.
3class Node:
4    def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):
5        self.val = val
6        self.isLeaf = isLeaf
7        self.topLeft = topLeft
8        self.topRight = topRight
9        self.bottomLeft = bottomLeft
10        self.bottomRight = bottomRight
11"""
12
13
14class Solution:
15    def intersect(self, quadTree1: "Node", quadTree2: "Node") -> "Node":
16        """
17        Performs logical OR operation on two quad trees.
18      
19        Args:
20            quadTree1: First quad tree node
21            quadTree2: Second quad tree node
22          
23        Returns:
24            Node: The resulting quad tree after OR operation
25        """
26      
27        def dfs(node1: "Node", node2: "Node") -> "Node":
28            """
29            Recursively compute the OR operation between two quad tree nodes.
30          
31            Args:
32                node1: First quad tree node
33                node2: Second quad tree node
34              
35            Returns:
36                Node: Merged quad tree node
37            """
38          
39            # Base case: both nodes are leaves
40            if node1.isLeaf and node2.isLeaf:
41                # Return a leaf node with OR of the two values
42                return Node(node1.val or node2.val, True, None, None, None, None)
43          
44            # If first node is a leaf
45            if node1.isLeaf:
46                # If node1 has value True, it dominates; otherwise use node2
47                return node1 if node1.val else node2
48          
49            # If second node is a leaf
50            if node2.isLeaf:
51                # If node2 has value True, it dominates; otherwise use node1
52                return node2 if node2.val else node1
53          
54            # Both nodes are internal nodes - recursively merge all quadrants
55            result = Node(False, False, None, None, None, None)
56            result.topLeft = dfs(node1.topLeft, node2.topLeft)
57            result.topRight = dfs(node1.topRight, node2.topRight)
58            result.bottomLeft = dfs(node1.bottomLeft, node2.bottomLeft)
59            result.bottomRight = dfs(node1.bottomRight, node2.bottomRight)
60          
61            # Check if all children are leaves
62            all_children_are_leaves = (
63                result.topLeft.isLeaf
64                and result.topRight.isLeaf
65                and result.bottomLeft.isLeaf
66                and result.bottomRight.isLeaf
67            )
68          
69            # Check if all children have the same value
70            all_children_same_value = (
71                result.topLeft.val
72                == result.topRight.val
73                == result.bottomLeft.val
74                == result.bottomRight.val
75            )
76          
77            # If all children are leaves with same value, merge into a single leaf
78            if all_children_are_leaves and all_children_same_value:
79                result = result.topLeft
80          
81            return result
82      
83        return dfs(quadTree1, quadTree2)
84
1/*
2// Definition for a QuadTree node.
3class Node {
4    public boolean val;
5    public boolean isLeaf;
6    public Node topLeft;
7    public Node topRight;
8    public Node bottomLeft;
9    public Node bottomRight;
10
11    public Node() {}
12
13    public Node(boolean _val, boolean _isLeaf, Node _topLeft, Node _topRight, 
14                Node _bottomLeft, Node _bottomRight) {
15        val = _val;
16        isLeaf = _isLeaf;
17        topLeft = _topLeft;
18        topRight = _topRight;
19        bottomLeft = _bottomLeft;
20        bottomRight = _bottomRight;
21    }
22};
23*/
24
25class Solution {
26    /**
27     * Performs logical OR intersection of two quad trees.
28     * @param quadTree1 First quad tree
29     * @param quadTree2 Second quad tree
30     * @return The resulting quad tree after intersection
31     */
32    public Node intersect(Node quadTree1, Node quadTree2) {
33        return performIntersection(quadTree1, quadTree2);
34    }
35
36    /**
37     * Recursively performs the intersection operation on two quad tree nodes.
38     * @param node1 First quad tree node
39     * @param node2 Second quad tree node
40     * @return The intersected node
41     */
42    private Node performIntersection(Node node1, Node node2) {
43        // Case 1: Both nodes are leaves - perform OR operation on their values
44        if (node1.isLeaf && node2.isLeaf) {
45            return new Node(node1.val || node2.val, true);
46        }
47      
48        // Case 2: First node is a leaf
49        if (node1.isLeaf) {
50            // If node1's value is true (1), the entire region is 1, return node1
51            // Otherwise, return node2's structure
52            return node1.val ? node1 : node2;
53        }
54      
55        // Case 3: Second node is a leaf
56        if (node2.isLeaf) {
57            // If node2's value is true (1), the entire region is 1, return node2
58            // Otherwise, return node1's structure
59            return node2.val ? node2 : node1;
60        }
61      
62        // Case 4: Both nodes have children - recursively intersect all quadrants
63        Node resultNode = new Node();
64        resultNode.topLeft = performIntersection(node1.topLeft, node2.topLeft);
65        resultNode.topRight = performIntersection(node1.topRight, node2.topRight);
66        resultNode.bottomLeft = performIntersection(node1.bottomLeft, node2.bottomLeft);
67        resultNode.bottomRight = performIntersection(node1.bottomRight, node2.bottomRight);
68      
69        // Check if all four children are leaves
70        boolean allChildrenAreLeaves = resultNode.topLeft.isLeaf && 
71                                       resultNode.topRight.isLeaf && 
72                                       resultNode.bottomLeft.isLeaf && 
73                                       resultNode.bottomRight.isLeaf;
74      
75        // Check if all four children have the same value
76        boolean allChildrenHaveSameValue = resultNode.topLeft.val == resultNode.topRight.val &&
77                                           resultNode.topRight.val == resultNode.bottomLeft.val && 
78                                           resultNode.bottomLeft.val == resultNode.bottomRight.val;
79      
80        // If all children are leaves with the same value, merge them into a single leaf node
81        if (allChildrenAreLeaves && allChildrenHaveSameValue) {
82            resultNode = resultNode.topLeft;
83        }
84      
85        return resultNode;
86    }
87}
88
1/*
2// Definition for a QuadTree node.
3class Node {
4public:
5    bool val;
6    bool isLeaf;
7    Node* topLeft;
8    Node* topRight;
9    Node* bottomLeft;
10    Node* bottomRight;
11
12    Node() {
13        val = false;
14        isLeaf = false;
15        topLeft = NULL;
16        topRight = NULL;
17        bottomLeft = NULL;
18        bottomRight = NULL;
19    }
20
21    Node(bool _val, bool _isLeaf) {
22        val = _val;
23        isLeaf = _isLeaf;
24        topLeft = NULL;
25        topRight = NULL;
26        bottomLeft = NULL;
27        bottomRight = NULL;
28    }
29
30    Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {
31        val = _val;
32        isLeaf = _isLeaf;
33        topLeft = _topLeft;
34        topRight = _topRight;
35        bottomLeft = _bottomLeft;
36        bottomRight = _bottomRight;
37    }
38};
39*/
40
41class Solution {
42public:
43    /**
44     * Performs logical OR operation on two quadtrees.
45     * @param quadTree1 First quadtree
46     * @param quadTree2 Second quadtree
47     * @return A new quadtree representing the logical OR of the two input quadtrees
48     */
49    Node* intersect(Node* quadTree1, Node* quadTree2) {
50        return mergeQuadTrees(quadTree1, quadTree2);
51    }
52
53private:
54    /**
55     * Recursively merges two quadtree nodes using logical OR operation.
56     * @param node1 First quadtree node
57     * @param node2 Second quadtree node
58     * @return Merged quadtree node
59     */
60    Node* mergeQuadTrees(Node* node1, Node* node2) {
61        // Case 1: Both nodes are leaf nodes
62        // Return a new leaf node with OR of their values
63        if (node1->isLeaf && node2->isLeaf) {
64            return new Node(node1->val || node2->val, true);
65        }
66      
67        // Case 2: First node is a leaf
68        // If it's true (all 1s), return it; otherwise return the second node
69        if (node1->isLeaf) {
70            return node1->val ? node1 : node2;
71        }
72      
73        // Case 3: Second node is a leaf
74        // If it's true (all 1s), return it; otherwise return the first node
75        if (node2->isLeaf) {
76            return node2->val ? node2 : node1;
77        }
78      
79        // Case 4: Both nodes are internal nodes
80        // Recursively merge all four quadrants
81        Node* mergedNode = new Node();
82        mergedNode->topLeft = mergeQuadTrees(node1->topLeft, node2->topLeft);
83        mergedNode->topRight = mergeQuadTrees(node1->topRight, node2->topRight);
84        mergedNode->bottomLeft = mergeQuadTrees(node1->bottomLeft, node2->bottomLeft);
85        mergedNode->bottomRight = mergeQuadTrees(node1->bottomRight, node2->bottomRight);
86      
87        // Check if all children are leaves with the same value
88        bool allChildrenAreLeaves = mergedNode->topLeft->isLeaf && 
89                                    mergedNode->topRight->isLeaf && 
90                                    mergedNode->bottomLeft->isLeaf && 
91                                    mergedNode->bottomRight->isLeaf;
92      
93        bool allChildrenHaveSameValue = mergedNode->topLeft->val == mergedNode->topRight->val && 
94                                        mergedNode->topRight->val == mergedNode->bottomLeft->val && 
95                                        mergedNode->bottomLeft->val == mergedNode->bottomRight->val;
96      
97        // If all children are leaves with the same value, merge them into a single leaf
98        if (allChildrenAreLeaves && allChildrenHaveSameValue) {
99            mergedNode = mergedNode->topLeft;
100        }
101      
102        return mergedNode;
103    }
104};
105
1/*
2// Definition for a QuadTree node.
3class Node {
4    val: boolean;
5    isLeaf: boolean;
6    topLeft: Node | null;
7    topRight: Node | null;
8    bottomLeft: Node | null;
9    bottomRight: Node | null;
10
11    constructor(
12        val?: boolean,
13        isLeaf?: boolean,
14        topLeft?: Node | null,
15        topRight?: Node | null,
16        bottomLeft?: Node | null,
17        bottomRight?: Node | null
18    ) {
19        this.val = val === undefined ? false : val;
20        this.isLeaf = isLeaf === undefined ? false : isLeaf;
21        this.topLeft = topLeft === undefined ? null : topLeft;
22        this.topRight = topRight === undefined ? null : topRight;
23        this.bottomLeft = bottomLeft === undefined ? null : bottomLeft;
24        this.bottomRight = bottomRight === undefined ? null : bottomRight;
25    }
26}
27*/
28
29/**
30 * Performs logical OR operation on two quadtrees.
31 * @param quadTree1 - First quadtree
32 * @param quadTree2 - Second quadtree
33 * @returns A new quadtree representing the logical OR of the two input quadtrees
34 */
35function intersect(quadTree1: Node | null, quadTree2: Node | null): Node | null {
36    return mergeQuadTrees(quadTree1, quadTree2);
37}
38
39/**
40 * Recursively merges two quadtree nodes using logical OR operation.
41 * @param node1 - First quadtree node
42 * @param node2 - Second quadtree node
43 * @returns Merged quadtree node
44 */
45function mergeQuadTrees(node1: Node | null, node2: Node | null): Node | null {
46    // Handle null cases
47    if (!node1 || !node2) {
48        return null;
49    }
50  
51    // Case 1: Both nodes are leaf nodes
52    // Return a new leaf node with OR of their values
53    if (node1.isLeaf && node2.isLeaf) {
54        return new Node(node1.val || node2.val, true);
55    }
56  
57    // Case 2: First node is a leaf
58    // If it's true (all 1s), return it; otherwise return the second node
59    if (node1.isLeaf) {
60        return node1.val ? node1 : node2;
61    }
62  
63    // Case 3: Second node is a leaf
64    // If it's true (all 1s), return it; otherwise return the first node
65    if (node2.isLeaf) {
66        return node2.val ? node2 : node1;
67    }
68  
69    // Case 4: Both nodes are internal nodes
70    // Recursively merge all four quadrants
71    const topLeft = mergeQuadTrees(node1.topLeft, node2.topLeft);
72    const topRight = mergeQuadTrees(node1.topRight, node2.topRight);
73    const bottomLeft = mergeQuadTrees(node1.bottomLeft, node2.bottomLeft);
74    const bottomRight = mergeQuadTrees(node1.bottomRight, node2.bottomRight);
75  
76    // Check if all children exist and are leaves with the same value
77    const allChildrenExist = topLeft && topRight && bottomLeft && bottomRight;
78  
79    if (allChildrenExist) {
80        const allChildrenAreLeaves = topLeft.isLeaf && 
81                                     topRight.isLeaf && 
82                                     bottomLeft.isLeaf && 
83                                     bottomRight.isLeaf;
84      
85        const allChildrenHaveSameValue = topLeft.val === topRight.val && 
86                                         topRight.val === bottomLeft.val && 
87                                         bottomLeft.val === bottomRight.val;
88      
89        // If all children are leaves with the same value, merge them into a single leaf
90        if (allChildrenAreLeaves && allChildrenHaveSameValue) {
91            return new Node(topLeft.val, true);
92        }
93    }
94  
95    // Create a new internal node with the merged children
96    const mergedNode = new Node(false, false, topLeft, topRight, bottomLeft, bottomRight);
97    return mergedNode;
98}
99

Time and Space Complexity

Time Complexity: O(n²) where n × n is the size of the grid represented by the QuadTree.

The algorithm performs a depth-first traversal of both QuadTrees simultaneously. In the worst case, when both trees are fully expanded (no leaf nodes except at the deepest level), we visit every node in both trees. For an n × n grid, a fully expanded QuadTree has approximately O(n²) nodes since:

  • Level 0: 1 node
  • Level 1: 4 nodes
  • Level 2: 16 nodes
  • Level k: 4^k nodes

The total number of nodes is 1 + 4 + 16 + ... + 4^(log₄(n²)) = O(n²). Each node is visited once and operations at each node take O(1) time.

Space Complexity: O(log n) for the recursion stack depth, plus O(n²) for the output tree.

The recursion depth is bounded by the height of the QuadTree, which is O(log n) for an n × n grid. The recursive calls use the call stack, contributing O(log n) space.

The output QuadTree in the worst case can have O(n²) nodes (when the result cannot be compressed into larger leaf nodes), requiring O(n²) space to store the result.

Therefore, the total space complexity is O(n²) dominated by the output size.

Learn more about how to find time and space complexity quickly.

Common Pitfalls

1. Incorrect Node Creation Without Proper Copying

The Pitfall: When handling leaf nodes in cases 2 and 3, directly returning references to the input nodes (return t1 or return t2) can lead to issues if the problem requires independent trees or if there's any subsequent tree modification. This creates shared references between the input and output trees.

Example of the Problem:

# Problematic code
if t1.isLeaf:
    return t1 if t1.val else t2  # Returns reference to original node

If the returned tree is later modified, it would inadvertently modify the original input trees as well.

Solution: Create new node instances instead of returning references:

if t1.isLeaf:
    if t1.val:
        # Create a new leaf node with value True
        return Node(True, True, None, None, None, None)
    else:
        # Deep copy t2 to ensure independence
        return deepcopy(t2)

2. Forgetting to Handle the Merge Optimization Correctly

The Pitfall: After creating the four children recursively, developers often forget to check if the resulting quadrants can be merged into a single leaf node. This results in unnecessarily complex trees with redundant internal nodes.

Example of Incorrect Implementation:

# Missing merge optimization
result = Node(False, False, None, None, None, None)
result.topLeft = dfs(node1.topLeft, node2.topLeft)
result.topRight = dfs(node1.topRight, node2.topRight)
result.bottomLeft = dfs(node1.bottomLeft, node2.bottomLeft)
result.bottomRight = dfs(node1.bottomRight, node2.bottomRight)
return result  # Returns without checking if children can be merged

Solution: Always check if all four children are identical leaves and merge them:

# After setting all four children
if (result.topLeft.isLeaf and result.topRight.isLeaf and 
    result.bottomLeft.isLeaf and result.bottomRight.isLeaf):
    if (result.topLeft.val == result.topRight.val == 
        result.bottomLeft.val == result.bottomRight.val):
        # Replace with a single leaf node
        return Node(result.topLeft.val, True, None, None, None, None)

3. Confusion Between OR and AND Operations

The Pitfall: The problem title often mentions "intersect" (which suggests AND operation), but the actual requirement is to perform OR operation. This naming confusion can lead to implementing the wrong logical operation.

Wrong Implementation:

# Incorrectly using AND instead of OR
if node1.isLeaf and node2.isLeaf:
    return Node(node1.val and node2.val, True, None, None, None, None)  # Wrong!

Solution: Ensure you're using the OR operation as specified:

if node1.isLeaf and node2.isLeaf:
    return Node(node1.val or node2.val, True, None, None, None, None)  # Correct

4. Not Handling None Nodes

The Pitfall: If the input trees can potentially have None nodes (though not typical in this problem), not checking for None can cause AttributeError.

Solution: Add None checks if necessary:

def dfs(node1, node2):
    if node1 is None:
        return node2
    if node2 is None:
        return node1
    # Rest of the logic...

5. Inefficient Deep Comparison for Merge Check

The Pitfall: Using complex equality checks or multiple nested conditions to verify if all children have the same value can make the code harder to read and potentially less efficient.

Inefficient Approach:

if (result.topLeft.val and result.topRight.val and 
    result.bottomLeft.val and result.bottomRight.val) or \
   (not result.topLeft.val and not result.topRight.val and 
    not result.bottomLeft.val and not result.bottomRight.val):
    # Merge logic

Solution: Use cleaner comparison:

values = [result.topLeft.val, result.topRight.val, 
          result.bottomLeft.val, result.bottomRight.val]
if len(set(values)) == 1:  # All values are the same
    # Merge logic
Discover Your Strengths and Weaknesses: Take Our 3-Minute Quiz to Tailor Your Study Plan:

Which algorithm is best for finding the shortest distance between two points in an unweighted graph?


Recommended Readings

Want a Structured Path to Master System Design Too? Don’t Miss This!

Load More