Leetcode: Symmetric Tree
LeetCode Tree
2023-09-11 14:14:08 时间
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the following is not: 1 / \ 2 2 \ \ 3 3 Note: Bonus points if you could solve it both recursively and iteratively.
难度:82. 这道题是树的题目,本质上还是树的遍历。这里无所谓哪种遍历方式,只需要对相应结点进行比较即可。一颗树对称其实就是看左右子树是否对称,一句话就是左同右,右同左,结点是对称的相等。不对称的条件有以下三个:(1)左边为空而右边不为空;(2)左边不为空而右边为空;(3)左边值不等于右边值。根据这几个条件在遍历时进行判断即可。
1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public boolean isSymmetric(TreeNode root) { 12 if (root == null) return true; 13 return check(root.left, root.right); 14 } 15 16 public boolean check(TreeNode node1, TreeNode node2) { 17 if (node1 == null && node2 == null) return true; 18 else if (node1 == null && node2 != null) return false; 19 else if (node1 != null && node2 == null) return false; 20 else if (node1.val != node2.val) return false; 21 return check(node1.left, node2.right) && check(node1.right, node2.left); 22 } 23 }
Iterative的方式参考了网上的解法:
public boolean isSymmetric(TreeNode root) { if(root == null) return true; if(root.left == null && root.right == null) return true; if(root.left == null || root.right == null) return false; LinkedList<TreeNode> q1 = new LinkedList<TreeNode>(); LinkedList<TreeNode> q2 = new LinkedList<TreeNode>(); q1.add(root.left); q2.add(root.right); while(!q1.isEmpty() && !q2.isEmpty()) { TreeNode n1 = q1.poll(); TreeNode n2 = q2.poll(); if(n1.val != n2.val) return false; if(n1.left == null && n2.right != null || n1.left != null && n2.right == null) return false; if(n1.right == null && n2.left != null || n1.right != null && n2.left == null) return false; if(n1.left != null && n2.right != null) { q1.add(n1.left); q2.add(n2.right); } if(n1.right != null && n2.left != null) { q1.add(n1.right); q2.add(n2.left); } } return true; }
相关文章
- Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree
- Leetcode: Range Sum Query - Mutable && Summary: Segment Tree
- Leetcode: Closest Binary Search Tree Value
- Leetcode: Recover Binary Search Tree
- [LeetCode] Best Time to Buy and Sell Stock
- Binary Search Tree 以及一道 LeetCode 题目
- JS Leetcode 374. 猜数字大小 题解分析
- [LeetCode] Binary Tree Maximum Path Sum
- [leetCode] Recover Binary Search Tree
- [LeetCode] Maximum Depth of Binary Tree
- LeetCode Merge Two Sorted Lists
- 【LeetCode】208. Implement Trie (Prefix Tree)
- 【LeetCode】199. Binary Tree Right Side View
- 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
- 【LeetCode OJ 232】Implement Queue using Stacks
- [LeetCode]Subsets II生成组合序列
- Leetcode Two Sum
- [LeetCode] 1145. Binary Tree Coloring Game 二叉树着色游戏
- [LeetCode] 1130. Minimum Cost Tree From Leaf Values 叶值的最小代价生成树
- [LeetCode] 979. Distribute Coins in Binary Tree 在二叉树中分配硬币
- [LeetCode] 971. Flip Binary Tree To Match Preorder Traversal 翻转二叉树以匹配先序遍历
- [LeetCode] 428. Serialize and Deserialize N-ary Tree N叉搜索树的序列化和去序列化
- [LeetCode] Second Minimum Node In a Binary Tree 二叉树中第二小的结点
- [LeetCode] 663. Equal Tree Partition 划分等价树
- [LeetCode] IPO 上市
- [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
- [LeetCode] 156. Binary Tree Upside Down 二叉树的上下颠倒
- leetcode 53. Maximum Subarray 最大子数组和(中等)