Determine whether the binary image

 

  • var isSymmetric = function (root) {  
  •     if (root == null) {  
  •         return true;  
  •     }  
  •     // left, right sequence of recursive binary tree  
  •     was  nums = [];  
  •     search(nums, root, 1);  
  •     // determine whether symmetry  
  •     var i = 0, j = nums.length - 1;  
  •     while (i < j) {  
  •         if (nums[i] != nums[j]) {  
  •             return false;  
  •         }  
  •         i++;  
  •         j--;  
  •     }  
  •     returntrue;    
  • };  
  • /** 
  •  * According to the order of left, right recursive binary tree, output to the array nums 
  •  * @Param nums output 
  •  * @Param n nodes 
  •  * @Param k level 
  •  */  
  • function search(nums, n, k) {  
  •     //left  
  •     if (n.left != null) {  
  •         search(nums, n.left, k + 1);  
  •     }  
  •     // node value hierarchy  
  •     nums.push(n.val + ',' + k);  
  •     //right  
  •     if (n.right != null) {  
  •         search(nums, n.right, k + 1);  
  •     }  
  • }  

       

    来自 <http://www.planetb.ca/projects/syntaxHighlighter/popup.php>

Guess you like

Origin www.cnblogs.com/xukaiae86/p/11723176.html