【leetcode】Tree——Symmetric Tree(101)

来源:互联网 发布:深圳市金蝶软件 编辑:程序博客网 时间:2024/06/06 06:45

题目:

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
思路:递归

代码:

public boolean isSymmetric(TreeNode root) {return root==null || isSymmetric(root.left, root.right);}private boolean isSymmetric(TreeNode left, TreeNode right){if(left==null||right==null)return left==right;if(left.val!=right.val)return false;return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);}


0 0
原创粉丝点击