101. 对称二叉树
101. 对称二叉树
https://leetcode-cn.com/problems/symmetric-tree/description/
package com.test; /** * @Author stono * @Date 2018/8/27 上午9:11 */ public class Lesson101 { public static void main(String[] args) { TreeNode t1 = new TreeNode(1); TreeNode t2L = new TreeNode(2); TreeNode t2R = new TreeNode(2); TreeNode t3_1 = new TreeNode(3); TreeNode t3_2 = new TreeNode(4); TreeNode t3_3 = new TreeNode(4); TreeNode t3_4 = new TreeNode(3); t1.left = t2L; t1.right = t2R; t2L.left = t3_1; t2L.right = t3_2; t2R.left = t3_3; t2R.right = t3_4; boolean symmetric = isSymmetric(t1); System.out.println(symmetric); } public static boolean isSymmetric(TreeNode root) { if (root == null) { return true; } return isSymmetric(root.left, root.right); } private static boolean isSymmetric(TreeNode left, TreeNode right) { if (left == null && right == null) { return true; } if (left != null && right == null) { return false; } if (left == null && right != null) { return false; } if (left.val - right.val != 0) { return false; } return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left); } }