1. 程式人生 > >leetcode -- 101. Symmetric Tree 【對稱樹,結構,內容】

leetcode -- 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:

    1
   / \
  2   2
 / \ / \
3  4 4  3

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

    1
   / \
  2   2
   \   \
   3    3

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

題意

給定一個二叉樹,檢查它是否是對稱的。

分析及解答

  • 結構,內容】結構上要一致,同時內容上要相同。

方法1(遞迴):

public boolean isSymmetric(TreeNode root) {
    return root==null || isSymmetricHelp(root.left, root.right);
}

private boolean isSymmetricHelp(TreeNode left, TreeNode right){
    if(left==null || right==null)
        return left==right;
    if(left.val!=right.val)
        return false;
    return isSymmetricHelp(left.left, right.right) && isSymmetricHelp(left.right, right.left);
}

方法2(非遞迴):
 public boolean isSymmetric(TreeNode root) {
        Queue<TreeNode> q = new LinkedList<TreeNode>();
        if(root == null) return true;
        q.add(root.left);
        q.add(root.right);
        while(q.size() > 1){
            TreeNode left = q.poll(),
                     right = q.poll();
            if(left== null&& right == null) continue;
            if(left == null ^ right == null) return false;
            if(left.val != right.val) return false;
            q.add(left.left);
            q.add(right.right);
            q.add(left.right);
            q.add(right.left);            
        }
        return true;
    }


方法2.2(較為複雜)

public boolean isSymmetric(TreeNode root) {
    if(root==null)  return true;
    
    Stack<TreeNode> stack = new Stack<TreeNode>();
    TreeNode left, right;
    if(root.left!=null){
        if(root.right==null) return false;
        stack.push(root.left);
        stack.push(root.right);
    }
    else if(root.right!=null){
        return false;
    }
        
    while(!stack.empty()){
        if(stack.size()%2!=0)   return false;
        right = stack.pop();
        left = stack.pop();
        if(right.val!=left.val) return false;
        
        if(left.left!=null){
            if(right.right==null)   return false;
            stack.push(left.left);
            stack.push(right.right);
        }
        else if(right.right!=null){
            return false;
        }
            
        if(left.right!=null){
            if(right.left==null)   return false;
            stack.push(left.right);
            stack.push(right.left);
        }


相關推薦

leetcode -- 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

leetcode 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 is symmetr

LeetCode 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 is symme

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 is symmet

[leetcode]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

LeetCode 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

LeetCode 101.Symmetric Tree (對稱二叉)

題目描述: 給定一個二叉樹,檢查它是否是映象對稱的。 例如,二叉樹 [1,2,2,3,4,4,3] 是對稱的。 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面這個 [1,2,2,null,3,null,3] 則不是映象對稱的

leetcode 101. Symmetric Tree(C語言二叉遞迴判對稱性)30

貼原題: 解析:   本題是給出一個二叉樹,讓判斷其是否左右對稱。   我的思路就是直接遞迴。新建一個遞迴函式,引數是其左右孩子節點,若兩個節點都存在且值相等則對稱,繼續比較其各自的左右孩子。

LeetCode 101. Symmetric Tree 對稱二叉

題目:給定一個二叉樹,檢查它是否是映象對稱的。例如,二叉樹 [1,2,2,3,4,4,3] 是對稱的。 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面這個 [1,2,2,null,3,null,3] 則不是映象對稱的: 1

leetcode 101.Symmetric Tree-對稱二叉|深度遍歷

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Fo

[LeetCode] Symmetric Tree 判斷對稱

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1

Leetcode 101. Symmetric Tree 驗證的對稱性 解題報告

1 解題思想 這道題是上一道題的延伸版。 首先,題目的意思是說給了一顆樹,讓我們判斷他是否是對稱的(以根節點為中心,映象顛倒而成) 其實解題方式也很簡單,把根節點的左右節點開始,當做兩個獨立的子樹,判斷這兩個子樹的是否對稱 而判斷這兩個子樹是否對稱,就是

leetcode--101. Symmetric Tree

win leet -- target 應該 efi iter int 開始 1、問題描述 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center

[Leetcode]101. Symmetric Tree

blog enter following lean 位置 his class 兩個 oot Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its cente

leetcode 101. Symmetric Tree

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * T

leetcode: 101. Symmetric Tree

Difficulty Easy. Problem # Given a binary tree, check whether it is a mirror of itself (ie, symmetric

Symmetric Tree對稱

題目描述 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric

[LeetCode 101] Symmetric Tree

import java.util.LinkedList; //判斷該二叉樹是否是對稱二叉樹 /** * Given a binary tree, check whether it is a mirror of itself (ie, symmetric ar

Leetcode--101. Symmetric Tree(easy)

sel nbsp right iter emp new spa etc ive    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

LeetCode-101. Symmetric Tree(Java)

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,