Thursday, August 21, 2014

Leetcode: Same Tree

Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.


Understand the problem:

The problem asks for checking if two trees are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value.  

Recursive Solution:
Note that for two trees are identical, both each node and the tree structures should be the same. So for each node, we first check if two node values are the same. Then we check both nodes have the same structure for left child and right child. We recursively repeat this process until we traversed all nodes of the tree.

Code (Java):
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        return isSame(p, q);
    }
    
    private boolean isSame(TreeNode p, TreeNode q) {
        if (p == null) return q == null;
        if (q == null) return false;
        
        if (p.val != q.val) return false;
        
        if (isSame(p.left, q.left) == false) return false;
        if (isSame(p.right, q.right) == false) return false;
        
        return true;
    }
} 


Iterative Solution:
The iterative solution still utilized queues. However, if we follow the previous BFS way using a queue, we will falsely consider 1,2 and 1,#,2 as the same tree. Why? Because we don't allow null in the queue. If we allow null in the queue, the problem will be much easier. 
We maintain a queue for each tree respectively. We dequeue a node from each tree, and check if both are null, we pop a new node. If either is a null, we return false. If both are not null and with different value, we return false as well. Then we add its left and right child into the queue at last.

Code (Java):
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) return true;
        
        Queue<TreeNode> lQueue = new LinkedList<TreeNode>();
        Queue<TreeNode> rQueue = new LinkedList<TreeNode>();
        
        lQueue.offer(p);
        rQueue.offer(q);
        if (lQueue.isEmpty() || rQueue.isEmpty()) return false;
        
        while (!lQueue.isEmpty() && !rQueue.isEmpty()) {
            TreeNode lCurr = lQueue.poll();
            TreeNode rCurr = rQueue.poll();
            
            if (lCurr == null && rCurr == null) continue;
            if (lCurr == null || rCurr == null) return false;
            
            if (lCurr.val != rCurr.val) return false;
            
            lQueue.offer(lCurr.left);
            lQueue.offer(lCurr.right);
            rQueue.offer(rCurr.left);
            rQueue.offer(rCurr.right);
        }
        
        
        return true;
    }
} 

Summary:
This question and the symmetric tree is the same kind of question. The recursive solution is very neat, but different from traditional DFS solution. The iterative solution using queue is a bit of tricky because we allow enqueue null into the queue, and utilized the null elements to check if the queue is the same. Be careful this idea. 

1 comment:

  1. Single queue Solution:

    public boolean isSameTree(TreeNode a, TreeNode b) {
    Queue queue = new LinkedList();
    queue.offer(a);
    queue.offer(b);

    while(!queue.isEmpty()){
    TreeNode p = queue.poll();
    TreeNode q = queue.poll();

    if(p == null && q == null) continue;
    if(p == null && q != null) return false;
    if(p != null && q == null) return false;

    if(p.val != q.val) return false;
    queue.offer(p.left);
    queue.offer(q.left);
    queue.offer(p.right);
    queue.offer(q.right);
    }

    return queue.isEmpty() ? true : false;
    }

    ReplyDelete