leetcode100 python the same tree __python

Source: Internet
Author: User

Given two binary trees, write a function to verify that they are the same.

If the two trees are structurally identical and the nodes have the same values, they are considered to be the same.

Example 1:

Input:       1         1
          /       /\
         2   3     2   3

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

output: True

Example 2:

Input:      1          1
          /           \
         2             2

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

output: false

Example 3:

Input:       1         1
          /       /\
         2   1     1   2

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

output: false

Python code

# 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 (SE  LF, p, Q:         "" "       : type P:treenode        : type  Q:treenode        : Rtype:bool         "" "        if not p And not Q: #两二叉树皆为空, recursive boundaries, both NULL returns true             return true         if p a nd Q and P.val==q.val:             L=self.issametree (p.left,q.left) #递归, each time again from the function entrance,  Recursive boundary judgment each time             R=self.issametree (p.right,q.right)             return L and R#and operations require that both L and R be true before returning true. Only the last recursive boundary return value         ELSE:     &NBSP
       Return False



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.