2017-10-01 4 views
0

2つのバイナリツリーt1とt2が与えられた場合、2つ目のツリーが最初のツリーのサブツリーであるかどうかを判断します。バイナリツリーtの頂点vのサブツリーは、vとそれに属するすべての子孫からなるツリーです。 t1の頂点v(おそらく空)のサブツリーがt2と等しくなるように、頂点v(おそらくはnone)が木t1にあるかどうかを決定する。codefights isSubTressただ1つのhide caseが渡せません

// 
    // Definition for binary tree: 
    // function Tree(x) { 
    // this.value = x; 
    // this.left = null; 
    // this.right = null; 
    // } 
    function isSubtree(t1, t2) { 

     function findroot(t1,t2){ 
     if(t2==null){ 
      return true; 
     }else if(t1==null){ 
      if(t2==null){ 
      return true; 
      }else{ 
      return false; 
      } 
     }else if((
      t1.value == t2.value && machedTree(t1,t2)) || findroot(t1.left,t2) || findroot(t1.right,t2)){ 
      return true; 
     }else{ 
      return false 
     } 
     } 
     function machedTree(t1,t2){ 
     if((t1 == null && t2 == null) 
      &&(t1 == null && t2==null)){ 
      return true; 
     }else if(t2 == null || 
      ((t1 != null && t2!=null) 
      && machedTree(t1.left,t2.left) 
      &&t1.value == t2.value 
      &&machedTree(t1.right,t2.right))){ 
      return true; 
     }else{ 
      return false; 
     } 

     } 
     return findroot(t1,t2) 
    } 

hideテストケースは1つしか通過できません。あなたは、コードのために間違っている任意のアイデアを持っているん

答えて

0

機能isSubtree(T1、T2){

 function findroot(t1,t2){ 
     if(t2==null){ 
      return true; 
     }else if(t1==null){ 
      return false; 
     }else if((
      t1.value == t2.value && machedTree(t1,t2)) || findroot(t1.left,t2) || findroot(t1.right,t2)){ 
      return true; 
     }else{ 
      return false 
     } 
     } 
     function machedTree(t1,t2){ 
     if(t1==null && t2 == null){ 
      return true; 
     } 
     else if((t2 != null && t1 != null) 
      && machedTree(t1.left,t2.left) 
      && (t1.value === t2.value) 
      &&machedTree(t1.right,t2.right)){ 
      return true; 
     }else{ 
      return false; 
     } 

     } 
     return findroot(t1,t2) 
    } 
関連する問題