Leetcode: Same Tree
2015-05-15 03:08
316 查看
Question
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.
Hide Tags Tree Depth-first Search
Analysis
It is similar as Symmetric Tree.Solution
[code]# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # @param {TreeNode} p # @param {TreeNode} q # @return {boolean} def isSameTree(self, p, q): return self.helper(p,q) def helper(self,root1,root2): if root1==None and root2==None: return True if root1==None or root2==None: return False if root1.val!=root2.val: return False return self.helper(root1.left,root2.left) and self.helper(root1.right, root2.right)
相关文章推荐
- 【LeetCode从零单刷】Same Tree
- leetcode笔记:Same Tree
- LeetCode---Same Tree
- 关于Leetcode中Same Tree一题的理解
- [LeetCode] [Same Tree 2012-09-03 ]
- LeetCode:Same Tree
- leetcode:100 Same Tree-每日编程第六题
- LeetCode之Same Tree
- LeetCode -- Same Tree
- [Leetcode]-Same Tree
- LeetCode:Same Tree
- [LeetCode] Same Tree
- leetcode Same Tree
- leetcode笔记:Same Tree
- 100. Same Tree Leetcode Python
- leetcode: Same Tree
- leetcode 100 —— Same Tree
- leetcode Same Tree
- leetcode--Same Tree
- LeetCode——same-tree