Judging whether the binary tree is mirror

?

Share a picture

Share a picture

  • var?isSymmetric?=?function?(root)?{??
  • ????if?(root?==?null strong>)?{??
  • ????????return?true;??
  • ????}??
  • ? ???//Left, middle and right order recursive binary tree??
  • ????var ?nums?=?[];??
  • ????search(nums,?root,?1);??
  • ????//Determine whether it is symmetrical??
  • ????var?i?=?0,?j?=?nums.length?-?1;?? < /span>
  • ????while?(i?
  • ?????? ??if?(nums[i]?!=?nums[j])?{??
  • ??????? ?????return?false ;??
  • ????????}??
  • ????????i++;??
  • ????????j–;??
  • ????}??
  • ????return?true;?? < /li>
  • };??
  • /**? < /span>
  • ?*? Recursive binary tree according to the left, middle and right order, Output to the array nums?
  • ?*[email protected]?nums?????? output?
  • ?*[email protected]?n????????? node ?
  • ?*[emailprotected]?k????????? level?
  • ?*/??
  • function ?search(nums,?n,?k)?{??
  • < li>????//Left??

  • ? ???if?(n.left?!=?null)?{??
  • < li>????????search(nums,?n.left,?k?+?1);?? < /li>

  • ????}??
  • ????//Node value, level??
  • ?? ??nums.push(n.val?+?‘,’?+?k);??
  • ????//Right??
  • ????if?(n.right?!= ?null)?{??
  • ????????search(nums,?n.right,?k?+?1);??
  • ????}??
  • }??

    ? ?

    From

}??

Leave a Comment

Your email address will not be published.