leetcode第101题对称二叉树

2018-06-11  本文已影响0人  CoderAPang

方法一:递归

/**
 * 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) {
        if(root == null)return true;
        return isMirror(root.left,root.right);
    }
    public boolean isMirror(TreeNode p,TreeNode q){
        if(p==null&q==null)return true;
        if(q!=null&p==null)return false;
        if(p!=null&q==null)return false;
        return (p.val==q.val&isMirror(p.left,q.right)&isMirror(p.right,q.left));
    }
}

[题目链接][1]
[1]:https://leetcode-cn.com/problems/symmetric-tree/description/

上一篇下一篇

猜你喜欢

热点阅读