zoukankan      html  css  js  c++  java
  • 剑指offer 对称的二叉树

    剑指offer 牛客网 对称的二叉树

    说明:递归方式调试好了,非递归的方式有问题,尚未解决

    # -*- coding: utf-8 -*-
    """
    Created on Wed Apr 10 14:18:42 2019
    
    题目:
        请实现一个函数,用来判断一颗二叉树是不是对称的。
        注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
    思路:
        递归方法:左右子节点递归遍历        
        非递归方法:按层序遍历的思路解决
    """
    
    # -*- coding:utf-8 -*-
    class TreeNode:
        def __init__(self, x):
            self.val = x
            self.left = None
            self.right = None
    class Solution:
        #非递归的方式,采用层序遍历的方式   (没有调试好,有问题,后期调试)
        def isSymmetrical(self, pRoot):
            # write code here
            if not pRoot:                           #如果为空节点,则为对称的               
                return True
            else:                                   
                temp = []                           #存放其各个节点的值
                index = 0
                temp.append(pRoot)                  #将头结点放入其中
                while index < len(temp):            #当下标超过其对应的长度时候便退出循环
                    len_son = len(temp)
                    while index < len_son:          #将当前下标的左右子节点加入其中
                        if temp[index]:             #判断当前节点是否为空节点
                            if temp[index].left:    #判断其左节点是否存在
                                temp.append(temp[index].left)#将其左节点加入其中
                            else:                   #不存在左子节点加入None
                                temp.append(None)
                        else:
                            temp.append(None)       #否则为叶子节点,加入两个空节点
                            temp.append(None)
                        if temp[index]:
                            if temp[index].right:
                                temp.append(temp[index].right)
                            else:
                                temp.append(None)
                        else:
                            temp.append(None)
                            temp.append(None)
                        index += 1
                                                    
                    pre_index = index + 1           
                    tail_index = len(temp)-1
                    while pre_index < tail_index:   #比较,两边往中间缩
                        if temp[pre_index].val != temp[tail_index].val:
                            return False
                        pre_index += 1
                        tail_index -= 1
                print(temp)
                return True
        #采用递归的方式
        def RisSymmetrical(self, pRoot):
            if not pRoot:                           #如果为空节点,是对称的
                return True
            if pRoot.left and not pRoot.right:      #如果有左没有右,则是不对称的
                return False
            if pRoot.right and not pRoot.left:      #如果有右没有左,则是不对称的
                return False
            def IsSame(left,right):
                if not left and not right:          #如果左右都为空节点,则为对称的
                    return True
                                                    #左右节点存在,并且其值相等,便递归的调用下去
                if (left and right) and left.val == right.val:
                                                    #传入(左左,右右) (左右,右左)
                    return IsSame(left.left,right.right) and IsSame(left.right,right.left)
            return IsSame(pRoot.left,pRoot.right)   #递归调用传入左节点和右节点
            
    if __name__ == '__main__':
        solution = Solution()
         #1 2 3 4 5 6 7 
        node_left = TreeNode(1)
        node_right = TreeNode(3)
        root_left = TreeNode(2)
        root_left.left = node_left
        root_left.right = node_right
        
        node_left = TreeNode(3)
        node_right = TreeNode(1)
        root_right = TreeNode(2)
        root = TreeNode(4)
        root.left = root_left
        root_right.left = node_left
        root_right.right = node_right
        root.right = root_right
        #非递归的方式
        res = solution.isSymmetrical(root)
        print(res)
        #递归的方式
        res = solution.RisSymmetrical(root)
        print(res)
  • 相关阅读:
    解决NLPIR汉语分词系统init failed问题
    牛客小白月赛3---G 旅游(树形dp)
    蓝桥杯 能量项链 (区间dp)
    OpenJ_Bailian
    LeetCode#169 Majority Element
    LeetCode#171 Excel Sheet Column Number
    LeetCode#172 Factorial Trailing Zeroes
    this指针
    auto、register、extern以及static
    const与static
  • 原文地址:https://www.cnblogs.com/missidiot/p/10684687.html
Copyright © 2011-2022 走看看