Day7 Symmetric Tree

LeetCode101 . Symmetric Tree


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


意思是判断一颗二叉树是否为镜像树。

思路来自于上一题的判断两棵树是否为相同的树,对于判断镜像树的话,只需要判断这棵树的左子树和右子树是否在镜像上是相同的树就ok了。只需要对原代码稍作改动:判断相同树时总是判断两棵树的相同位置是否相同,现在只需要改成镜像上是否相同,比如应该判断树1的最左子结点和树2的最右子结点是否值相同。


Day7 Symmetric Tree