0

I am trying to solve the given problem using Breadth-first Search (I know Depth-first search will be best suited for this scenario but I just want to try out things)

My code seems to be working in case of other test-cases but fails in case of first test case. Please suggest some improvements in my code.

Problem Link - https://leetcode.com/problems/leaf-similar-trees/

My Code:

 /**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool leafSimilar(TreeNode* root1, TreeNode* root2) {
        vector<int> v1, v2;
        
        queue<TreeNode*> q1;
        queue<TreeNode*> q2;
        

        //Applying BFS for first tree
        q1.push(root1);
        while(!q1.empty())
        {
            int size = q1.size();
            for(int i=0;i<size;i++)
            {
                TreeNode* curr = q1.front();
                q1.pop();
                
              
                if(curr->left != NULL)
                    q1.push(curr->left);
                if(curr->right != NULL)
                    q1.push(curr->right);
                
                
                if(curr->left == NULL && curr->right == NULL)
                    v1.push_back(curr->val); 
            }
            
        }
        
        
        //Applying BFS for second tree
        q2.push(root2);
        while(!q2.empty())
        {
            int size = q2.size();
            for(int i=0;i<size;i++)
            {
                TreeNode* curr = q2.front();
                q2.pop();
                
                
                
                if(curr->left != NULL)
                    q2.push(curr->left);
                if(curr->right != NULL)
                    q2.push(curr->right);
                
                if(curr->left == NULL && curr->right == NULL)
                    v2.push_back(curr->val);
            }
            
        }
        
         if(v1.size() != v2.size())
            return false;
        
         for(int i=0;i<v1.size();i++)
         {
             if(v1[i] != v2[i])
                 return false;
         }
        
        
        return true;
    }
};
  • The best thing you can do is get that first test case, run the program in a debugger, input the test case, and step through the function keeping an eye out for the unexpected (like taking the wrong path or storing the wrong value). That'll be a bug. Or bad expectations. Either is bad and needs fixing. – user4581301 Jul 21 '21 at 17:18
  • Side note: For the first test case, I'd probably do something silly like feed in an empty test case. If the code can't handle no data, that's a pretty quick turn-around on the test cycle. – user4581301 Jul 21 '21 at 17:20
  • Breadth first search finds the leafs in the order 6, 9, 8, 7, 4 in the tree displayed in the first picture of the site. Btw: if you're basically repeating the same logic multiple times with just some variables replaced you should consider using a function. In your case you're repeating the exact same logic twice for both trees. – fabian Jul 21 '21 at 17:22
  • 2
    A depth-first search visits the leaves from left to right (or right to left) regardless of depth. A breadth-first search visits them from the top down, so you visit the deepest leaf last. Thus, if the leaves are at different depths in the two trees, breadth-first will give you the wrong answer. – molbdnilo Jul 21 '21 at 17:35
  • *"I know Depth-first search will be best suited for this scenario but I just want to try out things"*: then you should also consider that sometimes you have to give up on an idea and conclude that it was a bad one. BFS is not helpful at all for tackling this problem. – trincot Jul 21 '21 at 20:05
  • @trincot, Ya, this problem made me realise this :| – manas karlekar Jul 22 '21 at 03:33

1 Answers1

0

I tried dry running your code on example 1.

Content of v1 : 6,9,8,7,4

where as content of v2: 6,7,4,9,8

since you are doing BFS traversal, leaf node at lower depth will be pushed first into the result vector than the leaf node at the higher depth. so it would not maintain the order from left to right.

To maintain the order from left to right you should be using DFS Traversal, try using In-order Traversal of the tree.

BitMask
  • 314
  • 2
  • 9