leetcode:树(簡単)100同じツリー| 101対称ツリー

トピック100同じツリー

与えられた2つのバイナリツリーは、それらが同じかそうでないかどうかを確認するための関数を書きます。

彼らは構造的に同一であり、ノードが同じ値を持っている場合、2つのバイナリツリーが同じと考えられています。

例1:

Input:     1         1
          / \       / \
         2   3     2   3

        [1,2,3],   [1,2,3]

Output: true

例2:

Input:     1         1
          /           \
         2             2

        [1,2],     [1,null,2]

Output: false

例3:

Input:     1         1
          / \       / \
         2   1     1   2

        [1,2,1],   [1,1,2]

Output: false

思考

再帰

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if p == None and q == None:
            return True
        elif p == None or q == None:
            return False
            
        if p.val != q.val:
            return False
        if self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right):
            return True
        else:
            return False

 

トピック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

 

注:
あなたは両方の再帰的かつ反復的にそれを解決することができればボーナスポイント。

思考

Aは考えた:再帰的な方法は非常に簡単です:

    def sub(self,p,q):
        if p == None and q == None:
            return True
        elif p == None or q == None:
            return False
        
        if p.val != q.val:
            return False
        if self.sub(p.left,q.right) and self.sub(p.right,q.left):
            return True
        else:
            return False
        
    def isSymmetric(self, root: TreeNode) -> bool:
        if root == None:
            return True
        else: 
            return self.sub(root.left,root.right)

 

二つのアイデア:スタック方式:左後 - 右 - 左全体のサブツリー内の値、その後は右に従うスタックにルートシーケンス - 左 - 右の順トラバーサルルートサブツリー。

 def isSymmetric(self, root: TreeNode) -> bool:
        if root == None:
            return True
        
        l_root = root.left
        r_root = root.right
        
        if l_root == None and r_root == None:
            return True
        elif l_root == None or r_root == None:
            return False
        
        stack_root = []
        nums = []
        while(1):
            while(1):
                while(l_root.left):
                    stack_root.append(l_root)
                    l_root = l_root.left
                nums.append(l_root.val)
                if(l_root.right):
                    stack_root.append(l_root)
                    l_root = l_root.right
                else:
                    break
            
            if len(stack_root)>0:
                while l_root.right == None and len(stack_root)>0:
                    l_root = stack_root.pop()
                    nums.append(l_root.val)
                if l_root.right:
                    l_root = l_root.right
                else:
                    break
            else:
                break
                
        print(nums)
        
        nums1 = []
        while(1):
            while(1):
                while(r_root.right):
                    stack_root.append(r_root)
                    r_root = r_root.right
                nums1.append(r_root.val)
                if len(nums)>0:
                    if r_root.val != nums[0]:
                        return False
                    else:
                        nums = nums[1:]
                else:
                    return False
                if(r_root.left):
                    stack_root.append(r_root)
                    r_root = r_root.left
                else:
                    break
            
            if len(stack_root)>0:
                while r_root.left == None and len(stack_root)>0:
                    r_root = stack_root.pop()
                    nums1.append(r_root.val)
                    if len(nums)>0:
                        if r_root.val != nums[0]:
                            return False
                        else:
                            nums = nums[1:]
                    else:
                        return False
                if r_root.left:
                    r_root = r_root.left
                else:
                    break
            else:
                break

        return True
        

しかし、ツリーの左側が右にツリーを横断する前に横断するので、スタックを使用して、そのために遅いです

 

 

 

公開された45元の記事 ウォンの賞賛1 ビュー3355

おすすめ

転載: blog.csdn.net/qq_22498427/article/details/104586439