LeetCode101----对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3]
是对称的。
1 / \ 2 2 / \ / \ 3 4 4 3
但是下面这个 [1,2,2,null,3,null,3]
则不是镜像对称的:
1 / \ 2 2 \ \ 3 3
代码如下:
public class LeetCode101 { public boolean isSymmetric = true; public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public boolean isSymmetric(TreeNode root) { if (root == null) { return true; } isSymmetric(root.left, root.right); return isSymmetric; } public void isSymmetric(TreeNode left, TreeNode right) { if (left == null || right == null) { if (left != right) { isSymmetric = false; } return; } isSymmetric(left.left, right.right); isSymmetric(left.right, right.left); if (left != null && right != null) { if (left.val != right.val) { isSymmetric = false; } } } }
非递归代码如下所示:
public class NonLeetCode101 { public boolean isSymmetric = true; public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public boolean isSymmetric(TreeNode root) { if (root == null) { return true; } isSymmetric(root.left, root.right); return isSymmetric; } public void isSymmetric(TreeNode left, TreeNode right) { if (left == null || right == null) { if (left != right) { isSymmetric = false; } return; } if (left.val != right.val) { isSymmetric = false; return; } Stack<TreeNode> lStack = lStruct(left); Stack<TreeNode> rStack = rStruct(right); if (lStack.size() != rStack.size()) { isSymmetric = false; return; } System.out.println("栈大小:" + lStack.size()); while (lStack.size() > 0) { TreeNode n1 = lStack.pop(); TreeNode n2 = rStack.pop(); if (n1 == null || n2 == null) { if (n1 != n2) { isSymmetric = false; return; } } else if (n1 != null && n2 != null) { if (n1.val != n2.val) { isSymmetric = false; return; } } } } private Stack<TreeNode> lStruct(TreeNode root) { Stack<TreeNode> stack = new Stack<>(); Stack<TreeNode> reStack = new Stack<>(); stack.push(root); while (!stack.isEmpty()) { root = stack.pop(); reStack.push(root); if (root != null) { if (root.right != null) { stack.push(root.right); } else if (root.right == null) { stack.push(null); } if (root.left != null) { stack.push(root.left); } else if (root.left == null) { stack.push(null); } } } return reStack; } private Stack<TreeNode> rStruct(TreeNode root) { Stack<TreeNode> stack = new Stack<>(); Stack<TreeNode> reStack = new Stack<>(); stack.push(root); while (!stack.isEmpty()) { root = stack.pop(); reStack.push(root); if (root != null) { if (root.left != null) { stack.push(root.left); } else if (root.left == null) { stack.push(null); } if (root.right != null) { stack.push(root.right); } else if (root.right == null) { stack.push(null); } } } return reStack; } }
递归代码如下:
public class LeetCode101 { public boolean isSymmetric = true; public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public boolean isSymmetric(TreeNode root) { if (root == null) { return true; } isSymmetric(root.left, root.right); return isSymmetric; } public void isSymmetric(TreeNode left, TreeNode right) { if (left == null || right == null) { if (left != right) { isSymmetric = false; } return; } isSymmetric(left.left, right.right); isSymmetric(left.right, right.left); if (left != null && right != null) { if (left.val != right.val) { isSymmetric = false; } } } }