题目描述
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
我的解法
解题思路
判断一个树是不是对称二叉树,可以使用两个队列,一个储存root左边的子树,一个储存root右边的子树。
实现代码
1 | /** |
Runtime: 4 ms, faster than 84.31% of C++ online submissions for Symmetric Tree.
Memory Usage: 15 MB, less than 37.29% of C++ online submissions for Symmetric Tree.