153 101 對稱二叉樹

2022-05-02 18:03:08 字數 3429 閱讀 8674

給定乙個二叉樹,檢查它是否是映象對稱的。(隨便看看吧,這個題真滴難-只有最後兩個是對的前面的都不是對的)

# definition for a binary tree node.

class treenode(object):

def __init__(self, val=none, left=none, right=none):

self.val = val

self.left = left

self.right = right

class solution(object):

def issymmetric1(self, root):

"""這個方法適合滿二叉樹,獲取是空節點補treenode()的樹

:type root: treenode

:rtype: bool

"""temp = root

stack = list()

temp_list = # 機率元素個數

while temp or stack:

while temp:

if not temp.right and temp.left:

temp.right = treenode(none)

if temp.right and not temp.left:

temp.left = treenode(none)

temp = temp.left

cur = stack.pop()

temp = cur.right

return temp_list == temp_list[::-1]

def issymmetric2(self, root):

"""這個方法適合滿二叉樹,獲取是空節點補treenode()的樹

:type root: treenode

:rtype: bool

"""return self.presearch(root) == self.postsearch(root)

def presearch1(self, root):

temp = root

stack = list()

temp_list = # 機率元素個數

while temp or stack:

while temp:

temp = temp.left

cur = stack.pop()

temp = cur.right

return temp_list

def postsearch1(self, root):

temp = root

stack = list()

temp_list = # 機率元素個數

while temp or stack:

while temp:

temp = temp.right

cur = stack.pop()

temp = cur.left

return temp_list

def add_node(self, root):

if not root.right and root.left:

root.right = treenode()

if not root.left and root.right:

root.left = treenode()

if root.left:

self.add_node(root.left)

if root.right:

self.add_node(root.right)

def presearch(self, root):

print(root.val)

if root.left:

self.presearch(root.left)

if root.right:

self.presearch(root.right)

def midsearch(self, root):

if root.left:

self.midsearch(root.left)

print(root.val)

if root.right:

self.midsearch(root.right)

def postsearch(self, root):

if root.left:

self.postsearch(root.left)

if root.right:

self.postsearch(root.right)

print(root.val)

def issymmetric3(self, root):

""":type root: treenode

:rtype: bool

"""return self.check(root, root)

def check(self, left, right):

if not left and not right:

return true

if not left or not right:

return false

return left.val == left.val and self.check(left.left, right.right) and self.check(left.right, right.left)

def issymmetric(self, root):

""":type root: treenode

:rtype: bool

"""queue = list()

while queue:

left = queue.pop()

right = queue.pop()

if not left and not right:

continue

if (not left or not right) or (left.val != right.val):

return false

return true

if __name__ == '__main__':

root = treenode(1)

n1 = treenode(2)

n2 = treenode(2)

n3 = treenode(3)

# n4 = treenode(4)

n5 = treenode(3)

# n6 = treenode(4)

n1.left = n3

# n1.right = n4

n2.left = n5

# n2.right = n6

root.left = n1

root.right = n2

s1 = solution()

print(s1.issymmetric(root))

101 對稱二叉樹

給定乙個二叉樹,檢查它是否是映象對稱的。例如,二叉樹 1,2,2,3,4,4,3 是對稱的。1 2 2 3 4 4 3但是下面這個 1,2,2,null,3,null,3 則不是映象對稱的 1 2 2 3 3說明 如果你可以運用遞迴和迭代兩種方法解決這個問題,會很加分。思路 遞迴就用dfs,迭代是b...

20 對稱二叉樹

題目 給定乙個二叉樹,檢查它是否是映象對稱的。例如,二叉樹 1,2,2,3,4,4,3 是對稱的。說明 如果你可以運用遞迴和迭代兩種方法解決這個問題,會很加分。歷史總是驚人的相似,我遇到這道題還是沒寫出來,但是,網上找到的 瞬間懂了其思路,哎,繼續刷題吧 public static boolean ...

101 對稱二叉樹

給定乙個二叉樹,檢查它是否是映象對稱的。例如,二叉樹 1,2,2,3,4,4,3 是對稱的。1 2 2 3 4 4 3 但是下面這個 1,2,2,null,3,null,3 則不是映象對稱的 1 2 2 3 3說明 如果你可以運用遞迴和迭代兩種方法解決這個問題,會很加分。解題思路 對稱二叉樹可以理解...