美文网首页
101. Symmetric Tree

101. Symmetric Tree

作者: becauseyou_90cd | 来源:发表于2018-07-31 22:38 被阅读0次

https://leetcode.com/problems/symmetric-tree/description/
解题思路:

  1. node1. left == node2.right && node1.right == node2.left

代码:
class Solution {
public boolean isSymmetric(TreeNode root) {

    return preorder(root, root);
}
public boolean preorder(TreeNode node1, TreeNode node2){
    if(node1 == null && node2 == null) return true;
    if(node1 != null && node2 != null && node1.val == node2.val)
        return preorder(node1.left, node2.right) && preorder(node1.right, node2.left);
    return false;
}

}

相关文章

网友评论

      本文标题:101. Symmetric Tree

      本文链接:https://www.haomeiwen.com/subject/orukvftx.html