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

Leetcode - 对称二叉树

程序员文章站 2022-06-17 19:49:16
...

Leetcode - 对称二叉树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public bool IsSymmetric(TreeNode root) {
        
        if(root == null){
            return true;
        }

        TreeNode left = root.left;
        TreeNode right = root.right;
        return IsMirror(left,right);
        
    }

    public bool IsMirror(TreeNode node1,TreeNode node2) {
        if(node1 == null && node2 == null) return true;
        if(node1 == null || node2 == null) return false;
        if(node1.val == node2.val) {
            return IsMirror(node1.left,node2.right) && IsMirror(node1.right,node2.left);
        }else{
            return false;
        }

    }
}

Leetcode - 对称二叉树

相关标签: Leetcode练习