leetcode 101. Symmetric Tree BFS广度优先遍历+DFS深度优先遍历

绝地灬酷狼 2022-06-08 02:14 359阅读 0赞

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. 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.

这道题是判断二叉树是否对称,我这里使用的BFS广度优先遍历来做的。

代码如下:

  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import java.util.Queue;
  5. /*class TreeNode {
  6. int val;
  7. TreeNode left;
  8. TreeNode right;
  9. TreeNode(int x) { val = x; }
  10. }
  11. */
  12. public class Solution
  13. {
  14. public boolean isSymmetric(TreeNode root)
  15. {
  16. if(root==null)
  17. return true;
  18. Queue<TreeNode> myQueue = new LinkedList<>();
  19. myQueue.add(root);
  20. while(myQueue.isEmpty()==false)
  21. {
  22. int size=myQueue.size();
  23. List<Integer> list=new ArrayList<Integer>();
  24. for(int i=0;i<size;i++)
  25. {
  26. //如果当前的结点是null,仍然要添加为null,然后判断每一层的val是否是一个回文序列
  27. TreeNode tmp=myQueue.peek();
  28. if(tmp != null)
  29. {
  30. myQueue.add(tmp.left);
  31. myQueue.add(tmp.right);
  32. list.add(tmp.val);
  33. }else
  34. list.add(null);
  35. myQueue.poll();
  36. }
  37. if(checkSymmetric(list)==false)
  38. return false;
  39. }
  40. return true;
  41. }
  42. //检查list是否对回文序列(这个是包含null)
  43. private boolean checkSymmetric(List<Integer> list)
  44. {
  45. if(list.size()<=0)
  46. return true;
  47. else
  48. {
  49. int left=0;
  50. int right=list.size()-1;
  51. while(left < right)
  52. {
  53. Integer l=list.get(left);
  54. Integer r=list.get(right);
  55. if(l==null && r==null || l!=null && r!=null && l==r)
  56. {
  57. left++;
  58. right--;
  59. }
  60. else
  61. return false;
  62. }
  63. return true;
  64. }
  65. }
  66. }

下面是C++的做法,就是一个简单的BFS广度优先遍历的做法

代码如下:

  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <unordered_map>
  5. #include <set>
  6. #include <unordered_set>
  7. #include <queue>
  8. #include <stack>
  9. #include <string>
  10. #include <climits>
  11. #include <algorithm>
  12. #include <sstream>
  13. #include <functional>
  14. #include <bitset>
  15. #include <numeric>
  16. #include <cmath>
  17. #include <regex>
  18. #include <iomanip>
  19. #include <cstdlib>
  20. #include <ctime>
  21. using namespace std;
  22. /*
  23. struct TreeNode
  24. {
  25. int val;
  26. TreeNode *left;
  27. TreeNode *right;
  28. TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  29. };
  30. */
  31. class Solution
  32. {
  33. public:
  34. bool isSymmetric(TreeNode* root)
  35. {
  36. if (root == NULL)
  37. return true;
  38. return dfs(root->left, root->right);
  39. }
  40. bool dfs(TreeNode* left, TreeNode* right)
  41. {
  42. if (left == NULL && right != NULL || left != NULL && right == NULL)
  43. return false;
  44. else if (left == NULL&&right == NULL)
  45. return true;
  46. else if (left->val != right->val)
  47. return false;
  48. else
  49. return dfs(left->left, right->right) == true && dfs(left->right, right->left);
  50. }
  51. bool isSymmetricByBFS(TreeNode* root)
  52. {
  53. if (root == NULL)
  54. return true;
  55. queue<TreeNode*> que;
  56. que.push(root);
  57. while (que.empty() == false)
  58. {
  59. int size = que.size();
  60. vector<int> list;
  61. for (int i = 0; i < size; i++)
  62. {
  63. TreeNode* top = que.front();
  64. if (top != NULL)
  65. {
  66. que.push(top->left);
  67. que.push(top->right);
  68. list.push_back(top->val);
  69. }
  70. else
  71. list.push_back(NULL);
  72. que.pop();
  73. }
  74. if (isVaild(list) == false)
  75. return false;
  76. }
  77. return true;
  78. }
  79. bool isVaild(vector<int> v)
  80. {
  81. if (v.size() <= 1)
  82. return true;
  83. else
  84. {
  85. int i = 0, j = v.size() - 1;
  86. while (i < j)
  87. {
  88. if (v[i] != v[j])
  89. return false;
  90. else
  91. {
  92. i++;
  93. j--;
  94. }
  95. }
  96. return true;
  97. }
  98. }
  99. };

发表评论

表情:
评论列表 (有 0 条评论,359人围观)

还没有评论,来说两句吧...

相关阅读