您的位置:首页 > 编程语言 > Python开发

【LeetCode with Python】 Same Tree

2008-12-07 13:02 302 查看
博客域名:http://www.xnerv.wang

原题页面:https://oj.leetcode.com/problems/same-tree/

题目类型:递归,回溯,DFS

难度评价:★

本文地址:/article/1377553.html

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

判断两个二叉树是否相等。常见的二叉树递归回溯算法。

class Solution:

    # @param p, a tree node
    # @param q, a tree node
    # @return a boolean
    def isSameTree(self, p, q):
        if None == p and None == q:
            return True
        elif (None == p and None != q) or (None != p and None == q):
            return False
        else:
            return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: