101. Symmetric Tree 二叉树是否对称

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
   /   2   2
 / \ / 3  4 4  3

But the following [1,2,2,null,3,null,3] is not:

    1
   /   2   2
   \      3    3

Note:
Bonus points if you could solve it both recursively and iteratively.

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * public int val;
  5. * public TreeNode left;
  6. * public TreeNode right;
  7. * public TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public bool IsSymmetric(TreeNode root) {
  12. if (root == null) return true;
  13. Queue<TreeNode> queue = new Queue<TreeNode>();
  14. queue.Enqueue(root);
  15. while (queue.Count > 0) {
  16. int count = queue.Count;
  17. List<int?> nextLevel = new List<int?>();
  18. for (int i = 0; i < count; i++) {
  19. TreeNode curNode = queue.Dequeue();
  20. if (curNode.left != null) {
  21. queue.Enqueue(curNode.left);
  22. nextLevel.Add(curNode.left.val);
  23. } else {
  24. nextLevel.Add(null);
  25. }
  26. if (curNode.right != null) {
  27. queue.Enqueue(curNode.right);
  28. nextLevel.Add(curNode.right.val);
  29. } else {
  30. nextLevel.Add(null);
  31. }
  32. }
  33. if (!LevelIsSymmetric(nextLevel)){
  34. return false;
  35. }
  36. }
  37. return true;
  38. }
  39. public bool LevelIsSymmetric(List<int?> list) {
  40. int left = 0;
  41. int right = list.Count - 1;
  42. while (right > left) {
  43. if (list[left++] != list[right--]) {
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49. }

null

时间: 2024-08-29 02:22:24

101. Symmetric Tree 二叉树是否对称的相关文章

Leetcode 101 Symmetric Tree 二叉树

判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert Binary Tree,然后用Same Tree比较左右子树 而我的做法是改下Same Tree的函数,改动的是第27行 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNo

leetCode 101. Symmetric Tree 对称树

101. Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric:     1    /   2   2  / \ / 3  4 4  3 But the following [1,2,2,null,3,null,3]

&amp;lt;LeetCode OJ&amp;gt; 101. Symmetric Tree

101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficulty: Easy 给定一颗二叉树,检查是否镜像对称(环绕中心对称) Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this bin

LeetCode 101 Symmetric Tree (C)

题目: 101. Symmetric Tree QuestionEditorial Solution My Submissions Total Accepted: 135232 Total Submissions: 375037 Difficulty: Easy Contributors: Admin Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

【61】101. Symmetric Tree

101. Symmetric Tree Description Submission Solutions Add to List Total Accepted: 154374 Total Submissions: 414598 Difficulty: Easy Contributors: Admin Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

LeetCode 101. Symmetric Tree (对称树)

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following [1,2,2,null,3,null,3] is not: 1 / 2 2 \ 3 3 Note:Bonus

101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

给定一个二叉树,检查它是否是它自己的镜像(即,围绕它的中心对称).例如,这个二叉树 [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说明:如果你可以递归地和迭代地解决它就奖励你点数.详见:https://leetcode.com/problems/symmetric-tree/description

101. Symmetric Tree -- 判断树结构是否对称

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 recu

【LeetCode】101. Symmetric Tree

题目: 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