【LeetCode with Python】 Symmetric Tree
2014-07-06 15:34
309 查看
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/symmetric-tree/
题目类型:
难度评价:★
本文地址:/article/1377497.html
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
But the following is not:
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what
> read more on how binary tree is serialized on OJ.
原题页面:https://oj.leetcode.com/problems/symmetric-tree/
题目类型:
难度评价:★
本文地址:/article/1377497.html
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following is not:
1 / \ 2 2 \ \ 3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what
"{1,#,2,3}"means?
> read more on how binary tree is serialized on OJ.
class Solution: def checkSysmmetric(self, left, right): if None == left and None == right: return True elif None == left or None == right: return False elif left.val != right.val: return False return self.checkSysmmetric(left.left, right.right) and self.checkSysmmetric(left.right, right.left) # @param root, a tree node # @return a boolean def isSymmetric(self, root): if None == root: return True return self.checkSysmmetric(root.left, root.right)
相关文章推荐
- 【LeetCode with Python】 Swap Nodes in Pairs
- 【LeetCode with Python】 Single Number
- 【LeetCode with Python】 Best Time to Buy and Sell Stock II
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- 【LeetCode with Python】 Regular Expression Matching
- 【LeetCode with Python】 Decode Ways
- 【LeetCode with Python】 LRU Cache
- 【LeetCode with Python】 Binary Tree Preorder Traversal
- 【LeetCode with Python】 Insertion Sort List
- 【LeetCode with Python】 Generate Parentheses
- 【LeetCode with Python】 Same Tree
- 【LeetCode with Python】 Pow(x, n)
- 【LeetCode with Python】 Remove Duplicates from Sorted Array
- 【LeetCode with Python】 Binary Tree Inorder Traversal
- 【LeetCode with Python】 Maximum Subarray
- 【LeetCode with Python】 String to Integer (atoi)
- 【LeetCode with Python】 Remove Duplicates from Sorted List
- 【LeetCode with Python】 Merge Two Sorted Lists
- 【LeetCode with Python】 Maximum Depth of Binary Tree
- 【LeetCode with Python】 Remove Element