leetcode#100 Same Tree

정은경·2022년 6월 6일
0

알고리즘

목록 보기
69/125

1. 문제

2. 나의 풀이

2-1. divide and conquer

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def isSameTree(self, p, q):
        """
        :type p: TreeNode
        :type q: TreeNode
        :rtype: bool
        """
        
        # print(p)
        # print(q)
        
        def compare_trees(p_node, q_node, checkR):
            if p_node and q_node:
                # print(p_node.val, q_node.val)
                if p_node.val != q_node.val:
                    # print("F1")
                    checkR.append(False)
                if p_node.left and q_node.left:
                    compare_trees(p_node.left, q_node.left, checkR)
                elif p_node.left or q_node.left:
                    # print("F2")
                    checkR.append(False)
                if p_node.right and q_node.right:
                    compare_trees(p_node.right, q_node.right, checkR)
                elif p_node.right or q_node.right:
                    # print("F3")
                    checkR.append(False)
            
            elif p_node or q_node:
                # print("F4")
                checkR.append(False)
            
            return checkR
          
        print("result", compare_trees(p, q, []))
        result = compare_trees(p, q, [])
        if False in result:
            return False
        return True
        # return compare_trees(p, q, [])

3. 남의 풀이

profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글