Closed underwindfall closed 2 years ago
https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/
思路
[x] 刷15mins
[x] 刷5mins
// time O(n) // space O(n) class DFS { public boolean isSymmetric(TreeNode root) { if (root == null) { return true; } return dfs(root, root); } boolean dfs(TreeNode left, TreeNode right) { if (left == null && right == null) { return true; } if ((left != null & right == null) || (left == null && right != null)) { return false; } if (left.val == right.val) { return dfs(left.left, right.right) && dfs(left.right, right.left); } return false; } } // time O(n) // space O(n) class BFS { public boolean isSymmetric(TreeNode root) { Queue<TreeNode> q = new LinkedList<>(); if (root == null) { return true; } q.offer(root.left); q.offer(root.right); while (!q.isEmpty()) { TreeNode left = q.poll(); TreeNode right = q.poll(); if (left == null && right == null) { continue; } if (left == null || right == null) { return false; } if (left.val != right.val) { return false; } q.offer(left.left); q.offer(right.right); q.offer(left.right); q.offer(right.left); } return true; } }
https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/
思路
[x] 刷15mins
[x] 刷5mins