bool isSubtree(TreeNode* s, TreeNode* t) {
    if(t==NULL)
        return true;
    if(s==NULL)
        return false;
    return  isSame(s, t) || isSubtree(s->left, t) || isSubtree(s->right, t);
}
示例#2
0
 /**
  * @param T1, T2: The roots of binary tree.
  * @return: True if T2 is a subtree of T1, or false.
  */
 bool isSubtree(TreeNode *T1, TreeNode *T2) {
     if (isTreeIdentical(T1, T2)) return true;
     if (T1 == NULL) 
         return false;
     else 
         return isSubtree(T1->left, T2) || isSubtree(T1->right, T2);
 }
示例#3
0
 bool isSubtree(TreeNode* s, TreeNode* t) {
     if(t == nullptr) {
         return true;
     } else if(s == nullptr) {
         return false;
     }
     return isSameTree(s, t) || isSubtree(s->left, t) || isSubtree(s->right, t);
 }
 bool isSubtree(TreeNode* s, TreeNode* t) {
   if(s->left && s->right)
         return isSimilarTree(s,t) || isSubtree(s->left,t) || isSubtree(s->right,t);
     if(s->left && !s->right)
         return isSimilarTree(s,t) || isSubtree(s->left,t);
     if(!s->left && s->right)
         return isSimilarTree(s,t) || isSubtree(s->right,t);
     return isSimilarTree(s,t);
 }
  bool isSubtree(TreeNode* s, TreeNode* t)
  {
    if (!s && !t) return true;
    if (!s || !t) return false;

    return dfs(s, t) ||
        isSubtree(s->left, t) ||
        isSubtree(s->right, t);
  }
示例#6
0
    /**
     * @param T1, T2: The roots of binary tree.
     * @return: True if T2 is a subtree of T1, or false.
     */
    bool isSubtree(TreeNode *T1, TreeNode *T2) {
		if (sameTree(T1, T2)) {
			return true;
		}
		if (T1 == NULL) {
			return false;
		}
		return isSubtree(T1->left, T2) || isSubtree(T1->right, T2);
    }
    bool isSubtree(TreeNode* s, TreeNode* t) {
        // be careful about null pointer
        if (!s)
            return false;
        else if (isSameTree(s, t))
            return true;

        return isSubtree(s->left, t) || isSubtree(s->right, t);
    }
 /**
  * @param T1, T2: The roots of binary tree.
  * @return: True if T2 is a subtree of T1, or false.
  */
 bool isSubtree(TreeNode *T1, TreeNode *T2) {
     // write your code here
     if (!T2) {
         return true;
     } else if (!T1) {
         return false;
     } else {
         return isSameTree(T1, T2) || isSubtree(T1->left, T2) || isSubtree(T1->right, T2);
     }
 }
/* This function returns true if S is a subtree of T, otherwise false */
bool isSubtree(struct node *T, struct node *S)
{
    /* base cases */
    if (S == NULL)
        return true;

    if (T == NULL)
        return false;

    /* Check the tree with root as current node */
    if (areIdentical(T, S))
        return true;

    /* If the tree with root as current node doesn't match then
       try left and right subtrees one by one */
    return isSubtree(T->left, S) ||
           isSubtree(T->right, S);
}
示例#10
0
bool UnorderedTree::isSubtree(TreeNodes *a, TreeNodes *b){
   // UnOrdered Subtree Test
   setTreeNodes aTreeComponents=TreeComponents(a);
   setTreeNodes bTreeComponents=TreeComponents(b);

   if (aTreeComponents.size()>bTreeComponents.size()||a->size()>b->size())
       return false;
   if (*a==*b || a->size()<=1)
       return true;

   map<pair<TreeNodes,TreeNodes> , bool>::iterator itsub;
   itsub=Is_subtree_Dictionary.find(make_pair(*a,*b));
   if (itsub!=Is_subtree_Dictionary.end()) {
	return Is_subtree_Dictionary[make_pair(*a,*b)];}
   Graph graph; //graph={}  //Dictionary mapping members of U to a list of V : multimap<int,int>
   BipartiteGraph bipartite_matching_graph;
   setTreeNodes::iterator aiterset;
   setTreeNodes::iterator biterset;
   TreeNodes::iterator iteTreeNodes;
   int sa=0;

   for ( aiterset=aTreeComponents.begin();aiterset!=aTreeComponents.end();++aiterset,sa++){
        int sb=0; bool done=false;
        for (biterset=bTreeComponents.begin();biterset!=bTreeComponents.end();++biterset,sb++){
			TreeNodes aa=(*aiterset);
			TreeNodes bb=(*biterset);
            if (isSubtree(&aa,&bb)){
               graph.insert(make_pair(sa+1,sb+1));
			   if (!done && bipartite_matching_graph[sb+1]==0) {
                         done=true;
                         bipartite_matching_graph[sb+1] =sa+1;}
			   }
           }
        }

   for(int i=1;i<=aTreeComponents.size();i++) if (graph.count(i)==0) 
   {Is_subtree_Dictionary[make_pair(*a,*b)]=false; 
   return false;
   }

   //Matching_Dictionary;
   /*map<Graph,bool>::iterator itsub;
   itsub = Matching_Dictionary.find(graph);
   if (itsub!=Matching_Dictionary.end()) {
	return Matching_Dictionary[graph];}
   */
   bipartiteMatch(&graph, &bipartite_matching_graph); 

   bool ret=(bipartite_matching_graph.size()>=aTreeComponents.size()) ;  
   Is_subtree_Dictionary[make_pair(*a,*b)]=ret;
   /*Matching_Dictionary[graph]=ret;*/
   return ret;   
}
示例#11
0
bool UnorderedTree::isSubtreeUnOrdInc(TreeNodes *supertree, Integer *iNode){
	//UnOrdered version
	bool blret=false;
	TreeNodes t0;
	Integer Size0=supertree->size();
	Integer Size1=tree.size();
	if (Size0-*iNode<Size1) return false;
	while(blret==false && Size0-*iNode>=Size1){
		t0=SubtreeAtNode(supertree,iNode);
		blret=isSubtree(&tree,&t0);
		if (blret==false) (*iNode)++;
	}
	return blret; 
}	  
void MutationObserverRegistration::observedSubtreeNodeWillDetach(Node* node)
{
    if (!isSubtree())
        return;

    node->registerTransientMutationObserver(this);
    m_observer->setHasTransientRegistration();

    if (!m_transientRegistrationNodes) {
        m_transientRegistrationNodes = adoptPtr(new NodeHashSet);

        ASSERT(!m_registrationNodeKeepAlive);
        m_registrationNodeKeepAlive = m_registrationNode; // Balanced in clearTransientRegistrations.
    }
    m_transientRegistrationNodes->add(node);
}
void MutationObserverRegistration::observedSubtreeNodeWillDetach(Node& node)
{
    if (!isSubtree())
        return;

    node.registerTransientMutationObserver(this);
    m_observer->setHasTransientRegistration();

    if (!m_transientRegistrationNodes) {
        m_transientRegistrationNodes = adoptPtrWillBeNoop(new NodeHashSet);

        ASSERT(m_registrationNode);
        ASSERT(!m_registrationNodeKeepAlive);
        m_registrationNodeKeepAlive = PassRefPtrWillBeRawPtr<Node>(m_registrationNode.get()); // Balanced in clearTransientRegistrations.
    }
    m_transientRegistrationNodes->add(&node);
}
bool MutationObserverRegistration::shouldReceiveMutationFrom(Node* node, MutationObserver::MutationType type, const QualifiedName* attributeName) const
{
    ASSERT((type == MutationObserver::Attributes && attributeName) || !attributeName);
    if (!(m_options & type))
        return false;

    if (m_registrationNode != node && !isSubtree())
        return false;

    if (type != MutationObserver::Attributes || !(m_options & MutationObserver::AttributeFilter))
        return true;

    if (!attributeName->namespaceURI().isNull())
        return false;

    return m_attributeFilter.contains(attributeName->localName());
}
/* Driver program to test above function */
int main()
{
    /* Construct the following tree
              26
            /   \
          10     3
        /    \     \
      4      6      3
       \
        30
    */
    struct node *T        = newNode(26);
    T->right              = newNode(3);
    T->right->right       = newNode(3);
    T->left               = newNode(10);
    T->left->left         = newNode(4);
    T->left->left->right  = newNode(30);
    T->left->right        = newNode(6);

    /* Construct the following tree
          10
        /    \
      4      6
       \
        30
    */
    struct node *S    = newNode(10);
    S->right          = newNode(6);
    S->left           = newNode(4);
    S->left->right    = newNode(30);


    if( isSubtree(T, S) )
        printf("Tree S is subtree of tree T");
    else
        printf("Tree S is not a subtree of tree T");

    getchar();
    return 0;
}
示例#16
0
int main()
{
    Tree<int> tree;
    tree.insert(50);
    tree.insert(17);
    tree.insert(12);
    tree.insert(9);
    tree.insert(14);
    tree.insert(23);
    tree.insert(19);
    tree.insert(72);
    tree.insert(54);
    tree.insert(67);
    tree.insert(76);

    Tree<int> subtree;
    subtree.insert(12);
    subtree.insert(9);
    subtree.insert(14);

    std::cout << isSubtree(tree, subtree);

    return 0;
}
示例#17
0
 bool isSubtree(TreeNode* s, TreeNode* t) {
     return isSame(s, t) || (s && isSubtree(s->left, t)) ||
                            (s && isSubtree(s->right, t));
 }
 bool isSubtree(TreeNode* s, TreeNode* t) {
     if(!s) return false;
     if(helper(s, t)) return true;
     return isSubtree(s->left, t) || isSubtree(s->right, t);
 }
示例#19
0
文件: 572.c 项目: MingfeiPan/leetcode
bool isSubtree(struct TreeNode* s, struct TreeNode* t) {
    if(s==NULL) return false;
    if(isS(s, t)) return true;
    return isSubtree(s->left, t) || isSubtree(s->right, t);
}
 bool isSubtree(TreeNode* s, TreeNode* t) {
     if (t == NULL && s == NULL) return true;
     if (t == NULL || s == NULL) return false;
     if (match(s, t)) return true;
     return isSubtree(s->left, t) || isSubtree(s->right, t);
 }
   bool AppSceneEnum::processNode(AppNode * node)
   {
      // Helper method to help rot nodes that we find in the scene.

      // At this stage we do not need to collect all the nodes
      // because the tree structure will still be there when we
      // build the shape.  What we need to do right now is grab
      // the top of all the subtrees, any meshes hanging on the
      // root level (these will be lower detail levels, we don't
      // need to grab meshes on the sub-trees because they will
      // be found when we recurse into the sub-tree), the bounds
      // node, and any sequences.

      const char * name  = node->getName();
      const char * pname = node->getParentName();

      AppConfig::PrintDump(PDPass1,avar("Processing Node %s with parent %s\r\n", name, pname));

      AppSequence * seq = getSequence(node);
      if (seq)
      {         
         sequences.push_back(seq);
         return true;
      }

      if (node->isDummy())
         return false;

      if (isSubtree(node))
      {
         // Add this node to the subtree list...
         AppConfig::PrintDump(PDPass1,avar("Found subtree starting at Node \"%s\"\r\n",name));
         subtrees.push_back(node);
         return true;
      }

      // See if it is a bounding box.  If so, save it as THE bounding
      // box for the scene
      if (node->isBounds())
      {
         if (boundsNode)
         {
            setExportError("More than one bounds node found.");
            AppConfig::PrintDump(PDPass1,"More than one bounds node found.\r\n");
         }
         else
            AppConfig::PrintDump(PDPass1,"Bounding box found\r\n");
         boundsNode = node;
         return true;
      }

      // if we use this node, then be sure to return true so the caller doesn't delete it
      bool used = false;

      if (node->getNumMesh()!=0)
      {
         for (S32 i=0; i<node->getNumMesh(); i++)
         {
            AppMesh * mesh = node->getMesh(i);
            if (mesh->isSkin())
            {
               AppConfig::PrintDump(PDPass1,avar("Skin \"%s\" with parent \"%s\" added to entry list\r\n",mesh->getName(),pname));
               skins.push_back(mesh);
               used = true;
            }
            else
            {
               if (node->isParentRoot())
               {
                  AppConfig::PrintDump(PDPass1,avar("Mesh \"%s\" with parent \"%s\" added to entry list\r\n",mesh->getName(),pname));
                  meshNodes.push_back(node);
                  meshes.push_back(mesh);
                  used = true;
               }
            }
         }
         if (used)
            usedNodes.push_back(node);
      }
      return used;
   }