請實作一個函式,用來判斷一顆二叉樹是不是對稱的,注意,如果一個二叉樹同此二叉樹的鏡像是同樣的,定義其為對稱的,
分析:對稱二叉樹就是相對于中間的根左右兩邊對稱left.left==right.right&&left.right==right.left
/* function TreeNode(x) { this.val = x; this.left = null; this.right = null; } */ function isSymmetrical(pRoot) { // write code here if(pRoot===null){ return true } return check(pRoot.left,pRoot.right) } function check(left,right){ if(left===null){ return right===null } if(right===null){ return false } if(left.val!==right.val){ return false } return check(left.left,right.right)&&check(left.right) }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/24672.html
標籤:其他
下一篇:樹---按之字形列印二叉樹
