力扣 LeetCode 101. 对称二叉树(Day7:二叉树)
解题思路:
使用后序遍历,左右中,先左右孩子再中间节点,从下往上传递,这样的话,代码中的outside和inside才会有值
class Solution {
public boolean isSymmetric(TreeNode root) {
return compare(root.left, root.right);
}
public boolean compare(TreeNode left, TreeNode right) {
if (left == null && right != null) return false;
if (left != null && right == null) return false;
if (left == null && right == null) return true;
if (left.val != right.val) return false;
boolean outside = compare(left.left, right.right);
boolean inside = compare(left.right, right.left);
boolean res = outside && inside;
return res;
}
}
原文地址:https://blog.csdn.net/qq_61504864/article/details/143883840
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!