LeetCode 101 对称二叉树
/** Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; } } */ class Solution { public boolean isSymmetric(TreeNode root) { return def(root,root); } public boolean def(TreeNode left,TreeNode right){ if(leftnull&&rightnull){ return true; }if(left == null||right == null){ return false; }if(left.val!=right.val){ return false; }if(!def(left.left,right.right)){ return false; }else{ return def(left.right,right.left); } } }
总结
树节点不能进行值的比较,也返回不了boolean值,得用树结构调用里面的属性进行值的比较,之后返回比较结果的一个boolean值!!!!
|