简单 相关标签 相关企业
给你一个二叉树的根节点 root
, 检查它是否轴对称。
示例 1:
输入:root = [1,2,2,3,4,4,3] 输出:true
示例 2:
输入:root = [1,2,2,null,3,null,3] 输出:false
提示:
- 树中节点数目在范围
[1, 1000]
内 -100 <= Node.val <= 100
进阶:你可以运用递归和迭代两种方法解决这个问题吗?
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean isSymmetric(TreeNode root) { return judge(root.left,root.right) && judge(root.right,root.left); } public boolean judge(TreeNode root1, TreeNode root2){ if(root1==null && root2==null) return true; else if(root1!=null && root2!=null){ if (root1.val == root2.val) { return judge(root1.left, root2.right) && judge(root1.right, root2.left); } } return false; } }
标签:right,TreeNode,val,null,二叉树,对称,101,root,left From: https://www.cnblogs.com/ak918xp/p/18228224