LeetCodeDay15 —— 对称二叉树&二叉树的层次遍历

101. 对称二叉树

描述
  • 给定一个二叉树,检查它是否是镜像对称的。
示例
二叉树 [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
说明
- 如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
思路
  1. 类比两个相等的二叉树,两个树镜像对称,即左树的左子树和右树的右子树相等、左树的右子树和右树的左子树相等。
  2. 迭代版本,则利用一个栈来代替递归的作用。
    (参考地址)
class Solution_101_01 {
 public:
  bool isSymmetric(TreeNode* root) {
    if (root == NULL) return true;
    return isMirror(root->left, root->right);
  }
  bool isMirror(TreeNode* root1, TreeNode* root2) {
    // If both trees are emptu, then they are mirror images
    if (root1 == NULL && root2 == NULL) return true;

    // For two trees to be mirror images, the following three
    // conditions must be true
    // 1 - Their root node's key must be same
    // 2 - left subtree of left tree and right subtree
    //      of right tree have to be mirror images
    // 3 - right subtree of left tree and left subtree
    //      of right tree have to be mirror images
    if (root1 && root2 && root1->val == root2->val)
      return isMirror(root1->left, root2->right) &&
             isMirror(root1->right, root2->left);

    // if neither of above conditions is true then root1
    // and root2 are not mirror images
    return false;
  }
};

class Solution_101_02 {
 public:
  bool isSymmetric(TreeNode* root) {
    if (root == NULL) return true;
    return isMirror(root->left, root->right);
  }
  bool isMirror(TreeNode* root1, TreeNode* root2) {
    stack<TreeNode*> s;
    s.push(root1);
    s.push(root2);
    while (!s.empty()) {
      TreeNode* tmp1 = s.top();
      s.pop();
      TreeNode* tmp2 = s.top();
      s.pop();
      if (!tmp1 && !tmp2) continue;
      if (!tmp1 || !tmp2 || tmp1->val != tmp2->val) return false;
      s.push(tmp1->left);
      s.push(tmp2->right);
      s.push(tmp1->right);
      s.push(tmp2->left);
    }
    return true;
  }
};

102. 二叉树的层次遍历

描述
  • 给定一个二叉树,返回其按层次遍历的节点值。(即逐层地,从左到右访问所有节点)。
示例
给定二叉树: [3,9,20,null,null,15,7],
    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:
[
  [3],
  [9,20],
  [15,7]
]
思路
  1. 利用队列实现层次遍历,两个队列交替使用,保存“层”的信息。
  2. 优化上述解法中利用两个队列来解决“层”的问题。每次遍历时,先取元素的个数,相当于是获得了改成元素的个数,再遍历。
class Solution_102_01 {
 public:
  vector<vector<int>> levelOrder(TreeNode* root) {
    if (root == NULL) return vector<vector<int>>();
    vector<vector<int>> res;
    queue<TreeNode*> q1, q2;
    q1.push(root);
    while (!q1.empty() && !q2.empty()) {
      queue<TreeNode*>& curQ = q1.empty() ? q2 : q1;
      queue<TreeNode*>& bakQ = curQ == q1 ? q2 : q1;
      vector<int> tmp;
      while (!curQ.empty()) {
        TreeNode* node = curQ.front();
        curQ.pop();
        tmp.push_back(node->val);
        if (node->left) bakQ.push(node->left);
        if (node->right) bakQ.push(node->right);
      }
      res.push_back(tmp);
    }
    return res;
  }
};

class Solution_102_02 {
 public:
  vector<vector<int>> levelOrder(TreeNode* root) {
    if(!root) return {};
    vector<vector<int>> res;
    queue<TreeNode*> q;
    q.push(root);
    while(!q.empty()){
      int size = q.size();
      vector<int> level;
      while(size--){
        TreeNode* node = q.front();
        q.pop();
        level.push_back(node->val);
        if(node->left) q.push(node->left);
        if(node->right) q.push(node->right);
      }
      res.push_back(level);
    }
    return res;
  }
};
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 树的概述 树是一种非常常用的数据结构,树与前面介绍的线性表,栈,队列等线性结构不同,树是一种非线性结构 1.树的定...
    Jack921阅读 9,940评论 1 31
  • 数据结构第12讲 二叉树的层次遍历 二叉树的遍历一般有先序遍历、中序遍历和后序遍历,这三种遍历比较简单。今天我们讲...
    rainchxy阅读 9,009评论 0 1
  • 一直以来,我都很少使用也避免使用到树和图,总觉得它们神秘而又复杂,但是树在一些运算和查找中也不可避免的要使用到,那...
    24K男阅读 11,709评论 5 14
  • 四、树与二叉树 1. 二叉树的顺序存储结构 二叉树的顺序存储就是用数组存储二叉树。二叉树的每个结点在顺序存储中都有...
    MinoyJet阅读 5,513评论 0 7
  • 小羊拼命的往前跑,跑着跑着,它遇到了功夫熊猫,功夫熊猫打了一拳狼,狼就拔腿跑了。 功夫熊猫收留了小...
    周熙媛阅读 3,493评论 2 2