LeetCode101. Symmetric Tree

101.Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
給一個二叉樹,判斷它是否是中心對稱二叉樹。
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
image

But the following [1,2,2,null,3,null,3] is not:
image

Note:
Bonus points if you could solve it both recursively and iteratively.

public class TreeNode {     
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

因爲剛做了 LeetCode.NO100 SameTree ,所以一下就想到了用相似的方法,用棧的方法,只是遍歷順序不一樣,將二叉樹從根開始,左右分成兩棵子樹,然後一棵先遍歷左邊,另一棵先遍歷右邊即可。

public boolean isSymmetric(TreeNode root) {
    if(root == null) return true;
    if(root.left == null && root.right == null) return true;
    if(root.left == null || root.right == null) return false;
    TreeNode leftTree = root.left;
    TreeNode rightTree = root.right;
    Stack<TreeNode> stack_left = new Stack<>();
    Stack<TreeNode> stack_right = new Stack<>();        
    stack_left.push(leftTree);
    stack_right.push(rightTree);
    while(!stack_left.isEmpty() && !stack_right.isEmpty()){
        TreeNode left = stack_left.pop();
        TreeNode right = stack_right.pop();
        if(left.val != right.val) return false;
        if(left.left != null) stack_left.push(left.left);
        if(right.right != null) stack_right.push(right.right);
        if(stack_left.size() != stack_right.size()) return false;
        if(left.right != null) stack_left.push(left.right);
        if(right.left != null) stack_right.push(right.left);
        if(stack_left.size() != stack_right.size()) return false;
    }       
    return stack_left.size() == stack_right.size();
}

方法二:採用遞歸方法

public boolean isSymmetric1(TreeNode root){
    if(root == null) return true;
    else{
        return isMirror1(root.left, root.right);
    }
}
public boolean isMirror1(TreeNode left, TreeNode right){
    if(left == null && right == null) return true;
    if(left == null || right == null) return false;
    if(left.val == right.val){
        return isMirror1(left.left, right.right) && isMirror1(left.right, right.left);
    }
    return false;
}

方法三:
跟上面方法不同就是第一次調用 isMirror 方法,isMirror2(root, root),上面的方法是判斷兩棵樹是否是鏡像樹,
下面這個判斷一棵樹是否是鏡像樹。
分析時間複雜度:每個節點都會對比一次,所以總共運行次數是 O(n),n是樹的節點個數。
函數遞歸調用次數與樹的高度有關,最壞的情況,樹是線性樹,高度是n,因此棧的空間複雜度最壞是O(n)。

public boolean isSymmetric2(TreeNode root) {
    return isMirror2(root, root);
}
public boolean isMirror2(TreeNode t1, TreeNode t2) {
    if (t1 == null && t2 == null) return true;
    if (t1 == null || t2 == null) return false;
    return (t1.val == t2.val)
      && isMirror2(t1.right, t2.left)
      && isMirror2(t1.left, t2.right);
}

方法四:用隊列的方法
用隊列的方法:比較隊列中相鄰兩個元素結構、值是否相等。
分析時間複雜度:總共運行次數是O(n),n是樹的節點個數。
空間複雜度:O(n)

public boolean isSymmetric3(TreeNode root) {
    Queue<TreeNode> q = new LinkedList<>();
    q.add(root);
    q.add(root);
    while (!q.isEmpty()) {
        TreeNode t1 = q.poll();
        TreeNode t2 = q.poll();
        if (t1 == null && t2 == null) continue;
        if (t1 == null || t2 == null) return false;
        if (t1.val != t2.val) return false;
        q.add(t1.left);
        q.add(t2.right);
        q.add(t1.right);
        q.add(t2.left);
    }
    return true;
}
發佈了48 篇原創文章 · 獲贊 11 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章