【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.
判断两个二叉树是否相等。常见的二叉树递归回溯算法。
原题页面: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)
相关文章推荐
- [leetcode]Copy List with Random Pointer @ Python
- 【LeetCode with Python】 Copy List with Random Pointer
- 【LeetCode with Python】 N-Queens II
- 【LeetCode with Python】 Sort Colors
- 【LeetCode with Python】 Populating Next Right Pointers in Each Node
- 【LeetCode with Python】 Remove Duplicates from Sorted List II
- 【LeetCode with Python】 Longest Common Prefix
- 【LeetCode with Python】 Set Matrix Zeroes
- leetcode 日经贴,python,cpp code -Substring with Concatenation of All Words
- Container With Most Water Leetcode Python Java
- 【Leetcode】【python】Same Tree
- 【LeetCode with Python】 Spiral Matrix II
- [leetcode]Substring with Concatenation of All Words @ Python
- [LeetCode]题解(python):138-Copy List with Random Pointer
- 【LeetCode with Python】 Validate Binary Search Tree
- 【LeetCode with Python】 Pow(x, n)
- 【LeetCode with Python】 Binary Tree Inorder Traversal
- 【LeetCode with Python】 Evaluate Reverse Polish Notation
- 【LeetCode with Python】 Rotate Image
- 【LeetCode with Python】 Convert Sorted Array to Binary Search Tree