LeetCode 题解(223) : Symmetric Tree

来源:互联网 发布:c语言99乘法表位置 编辑:程序博客网 时间:2024/06/05 19:36

题目:

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   / \  2   2 / \ / \3  4 4  3

But the following is not:

    1   / \  2   2   \   \   3    3

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

题解:

循环写的比较难看。

C++版:

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    bool isSymmetric(TreeNode* root) {        if(root == NULL || (root->left == NULL && root->right == NULL))            return true;        return isSymmetric(root->left, root->right);    }        bool isSymmetric(TreeNode* first, TreeNode* second) {        if((first == NULL && second != NULL) || (first != NULL && second == NULL))            return false;        if(first == NULL && second == NULL)            return true;        if(first->val == second->val) {            return isSymmetric(first->left, second->right) && isSymmetric(first->right, second->left);        }        return false;    }};

Java版:

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public boolean isSymmetric(TreeNode root) {        if(root == null || (root.left == null && root.right == null))            return true;        return isSymmetric(root.left, root.right);    }        public boolean isSymmetric(TreeNode first, TreeNode second) {        if(first == null && second != null)            return false;        if(first != null && second == null)            return false;        if(first == null && second == null)            return true;        if(first.val == second.val) {            return isSymmetric(first.left, second.right) && isSymmetric(first.right, second.left);        }        return false;    }}

Python版:

# Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneimport copyclass Solution(object):    def isSymmetric(self, root):        """        :type root: TreeNode        :rtype: bool        """        if root == None or (root.left == None and root.right == None):            return True        if root.left != None and root.right != None and root.left.val == root.right.val:            ql1, ql2, qr1, qr2 = [root.left], [], [root.right], []            while len(ql1) != 0 or len(qr1) != 0:                for i in range(len(ql1)):                    if ql1[i].left != None and qr1[-1 - i].right != None:                        if ql1[i].left.val != qr1[-1 - i].right.val:                            return False                    if (ql1[i].left != None and qr1[-1 - i].right == None) or (ql1[i].left == None and qr1[-1 - i].right != None):                        return False                    if ql1[i].right != None and qr1[-1 - i].left != None:                        if ql1[i].right.val != qr1[-1 - i].left.val:                            return False                    if (ql1[i].right != None and qr1[-1 - i].left == None) or (ql1[i].right == None and qr1[-1 - i].left != None):                        return False                for i in ql1:                    if i.left != None:                        ql2.append(i.left)                    if i.right != None:                        ql2.append(i.right)                                        for i in qr1:                    if i.left != None:                        qr2.append(i.left)                    if i.right != None:                        qr2.append(i.right)                ql1 = copy.copy(ql2)                qr1 = copy.copy(qr2)                ql2 = []                qr2 = []            return True        else:            return False                                                


0 0
原创粉丝点击