欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

LeetCode 第101题 对称二叉树

程序员文章站 2024-01-11 11:53:46
...

LeetCode 第101题 对称二叉树

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