Leetcode 101. Symmetric Tree

来源:互联网 发布:凯立德找不到导航软件 编辑:程序博客网 时间:2024/05/14 18:13

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

Analysis: 判断左右的节点是否对称

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public boolean isSymmetric(TreeNode root) {      if(root == null)        return true;      return isSymmetric(root.left, root.right);    }    public boolean isSymmetric(TreeNode left, TreeNode right){      if(left == null && right == null)        return true;      if(left == null || right == null)        return false;      return left.val==right.val && isSymmetric(left.left, right.right)&& isSymmetric(left.right, right.left);    }}


0 0
原创粉丝点击