• [LeetCode]题解(python):100 Same Tree


    题目来源


    https://leetcode.com/problems/same-tree/

    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.


    题意分析


    Input: twon binary tree

    Output: equal?

    Conditions:判断两个二叉树是不是相等。


    题目思路


    逐一遍历,看是否相等。


    AC代码(Python)

     1 # Definition for a binary tree node.
     2 # class TreeNode(object):
     3 #     def __init__(self, x):
     4 #         self.val = x
     5 #         self.left = None
     6 #         self.right = None
     7 
     8 class Solution(object):
     9     def isSameTree(self, p, q):
    10         """
    11         :type p: TreeNode
    12         :type q: TreeNode
    13         :rtype: bool
    14         """
    15         def dfs(p, q):
    16             if p == None and q != None:
    17                 return False
    18             if p != None and q == None:
    19                 return False
    20             if p == None and q == None:
    21                 return True
    22             if p.val != q.val:
    23                 return False
    24             return dfs(p.left, q.left) and dfs(p.right, q.right)
    25         
    26         return dfs(p,q)
    27         
  • 相关阅读:
    Callable+Future
    采用socket传输文件
    大端序和小端序
    域名
    mycat实现读写分离
    mysql存储过程
    Mysql主从同步
    centos6.5上安装5.7版本的mysql
    Mycat分库分表
    通过队列实现进程间的通信
  • 原文地址:https://www.cnblogs.com/loadofleaf/p/5502249.html
Copyright © 2020-2023  润新知