101. Symmetric Tree 101.对称树

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

典型的因为是形状而递归(recursive)的题目。所以最好写个helper函数吧,更加方便表达。之后的参数可以事无巨细地传进去。这题的递归入口
就是左边、右边这样
这种可能是空,不一定有的节点,需要丢到递归函数中
//推测着写。
        if ((root.left.left.val != root.right.right.val)  || (root.left.right.val != root.right.left.val))

猜你喜欢

转载自www.cnblogs.com/immiao0319/p/12953003.html