LeetCode101 Symmetric Tree

来源:互联网 发布:西文数据库 编辑:程序博客网 时间:2024/06/08 05:33

详细见:leetcode.com/problems/symmetric-tree


Java Solution: github

package leetcode;/* * 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  3But the following [1,2,2,null,3,null,3] is not:    1   / \  2   2   \   \   3    3Note:Bonus points if you could solve it both recursively and iteratively. */import tools.TreeNode辅助.TreeNode;public class P101_SymmetricTree {static int N = Integer.MIN_VALUE;public static void main(String[] args) {TreeNode root = null;root = tools.TreeNode辅助.A_生成满二叉树(new int[] {//1,//2, 2,//3, 4, 4, 3,//5, 6, 7, 8, 8, 7, 6, 51,2, 2,3, 4, 4, 3,5, 6, 7, 8, 8, N, 6, 5});Solution s = new Solution();System.out.println(s.isSymmetric(root));}/* * AC * 1 ms */static class Solution {    public boolean isSymmetric(TreeNode root) {    if (root == null) {    return true;    }        return is_my(root.left, root.right);    }private boolean is_my(TreeNode left, TreeNode right) {if (left == null || right == null) {return left == null && right == null;}if (left.val != right.val) {return false;}return is_my(left.left, right.right) && is_my(left.right, right.left);}}}


C Solution: github

/*    url: leetcode.com/problems/symmetric-tree    AC 3ms 23.58%*/#include <stdio.h>#include <stdlib.h>struct TreeNode {    int val;    struct TreeNode *left;    struct TreeNode *right;};#define bool inttypedef struct TreeNode stn;typedef struct TreeNode * ptn;typedef ptn T;typedef struct al sal;typedef struct al * pal;struct al {    int capacity;    int size;    T* arr;};pal al_init(int capacity) {    pal l = (pal) malloc(sizeof(sal));    if (capacity < 1) return NULL;    l->arr = (T*) malloc(sizeof(T) * capacity);    l->capacity = capacity;    l->size = 0;    return l;}void al_expand_capacity(pal l) {    T* new_arr = (T*) malloc(sizeof(T) * (l->capacity * 2 + 1));    int i = 0;    for (i = 0; i < l->capacity; i ++)        new_arr[i] = l->arr[i];    free(l->arr);    l->arr = new_arr;    l->capacity = l->capacity * 2 + 1;}void al_add_last(pal l, T v) {    if (l->capacity == l->size) al_expand_capacity(l);    l->arr[l->size] = v;    l->size ++;}void al_free_all(pal l) {    free(l->arr);    free(l);}void swap(pal* l1, pal* l2) {    pal l = *l1;    *l1 = *l2;    *l2 = l;}int cmp(ptn l1, ptn l2) {    if (l1 == NULL || l2 == NULL)        return l1 == NULL && l2 == NULL;    return l1->val == l2->val;}bool isSymmetric(struct TreeNode* root) {    pal l1 = NULL, l2 = NULL;    int i = 0, j = 0;    if (root == NULL) return 1;    l1 = al_init(16);    l2 = al_init(16);    al_add_last(l1, root);    while (l1->size != 0) {        l2->size = 0;        for (i = 0; i < l1->size; i ++) {            if (l1->arr[i] != NULL) {                al_add_last(l2, l1->arr[i]->left);                al_add_last(l2, l1->arr[i]->right);            }        }        i = 0;        j = l2->size - 1;        while (i < j) {            if (! cmp(l2->arr[i], l2->arr[j]))                return 0;            i ++;            j --;        }        swap(&l1, &l2);    }    al_free_all(l1);    al_free_all(l2);    return 1;}


Python Solution: github

#coding=utf-8'''    url: leetcode.com/problems/symmetric-tree    @author:     zxwtry    @email:      zxwtry@qq.com    @date:       2017年4月26日    @details:    Solution: 59ms 30.66%'''class TreeNode(object):    def __init__(self, x):        self.val = x        self.left = None        self.right = Noneclass Solution(object):    def isS(self, l):        i, j = 0, len(l)-1        while i < j:            if (l[i] == None) ^ (l[j] == None): return False            if l[i] != None and l[i].val != l[j].val: return False            i, j = i+1, j-1        return True        def isSymmetric(self, root):        """        :type root: TreeNode        :rtype: bool        """        if root == None: return True        l1, s = [root], True        while s:            if (not self.isS(l1)): return False            l2, s = [], False            for n in l1:                if n == None: continue                s = True                l2.append(n.left)                l2.append(n.right)            l1 = l2        return True


0 0
原创粉丝点击