LeetCode之“树”:Symmetric Tree

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 recursively and iteratively.

  confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

  OJ‘s Binary Tree Serialization:

  The serialization of a binary tree follows a level order traversal, where ‘#‘ signifies a path terminator where no node exists below.

  Here‘s an example:

   1
  /  2   3
    /
   4
         5

  The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

  本题暂用递归解法,具体程序(4ms)如下:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     void updateLVec(TreeNode *left, vector<int>& lVec)
13     {
14         if(left)
15         {
16             lVec.push_back(left->val);
17             if(left->left || left->right)
18             {
19                 if(left->left)
20                     updateLVec(left->left, lVec);
21                 else
22                     lVec.push_back(INT_MIN);
23                 if(left->right)
24                     updateLVec(left->right, lVec);
25                 else
26                     lVec.push_back(INT_MIN);
27             }
28         }
29     }
30
31     void updateRVec(TreeNode *right, vector<int>& rVec)
32     {
33         if(right)
34         {
35             rVec.push_back(right->val);
36             if(right->left || right->right)
37             {
38                 if(right->right)
39                     updateRVec(right->right, rVec);
40                 else
41                     rVec.push_back(INT_MIN);
42                 if(right->left)
43                     updateRVec(right->left, rVec);
44                 else
45                     rVec.push_back(INT_MIN);
46             }
47         }
48     }
49
50     bool isSymmetric(TreeNode* root) {
51         if(!root || (!root->left && !root->right))
52             return true;
53
54         if((!root->left && root->right) || (root->left && !root->right))
55             return false;
56
57         vector<int> lVec, rVec;
58         updateLVec(root->left, lVec);
59         updateRVec(root->right, rVec);
60
61         int szLVec = lVec.size();
62         int szRVec = rVec.size();
63         if(szLVec != szRVec)
64             return false;
65
66         for(int i = 0; i < szLVec; i++)
67         {
68             if(lVec[i] != rVec[i])
69                 return false;
70         }
71
72         return true;
73     }
74 };

  虽然最终解决了问题,但代码还是复杂了。别人的代码(4ms)真叫一个简单呐:

 1 bool isSymmetric(TreeNode *root)
 2 {
 3     if (!root) return true;
 4     return isSymmetric(root->left, root->right);
 5 }
 6 bool isSymmetric(TreeNode *lt, TreeNode *rt)
 7 {
 8     if (!lt && !rt) return true;
 9     if (lt && !rt || !lt && rt || lt->val != rt->val) return false;
10     return isSymmetric(lt->left, rt->right) && isSymmetric(lt->right, rt->left);
11 }

Same Tree

  题目链接

  题目要求:

  Given two binary trees, write a function to check if they are equal or not.

  Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

  有了上一道题的基础,这道题就很简单了,具体程序(0ms)如下:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isSameTree(TreeNode* p, TreeNode* q) {
13         if(!p && !q)
14             return true;
15         return isSameTreeSub(p, q);
16     }
17
18     bool isSameTreeSub(TreeNode *lt, TreeNode *rt)
19     {
20         if(!lt && !rt)
21             return true;
22         if((!lt && rt) || (lt && !rt) || (lt->val != rt->val))
23             return false;
24         return isSameTree(lt->left, rt->left) && isSameTree(lt->right, rt->right);
25     }
26 };
时间: 2024-11-08 00:00:43

LeetCode之“树”:Symmetric Tree的相关文章

Leetcode 树 Symmetric Tree

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Symmetric Tree Total Accepted: 13991 Total Submissions: 44240 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmet

LeetCode OJ:Symmetric Tree 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 判断一颗树是否对称,首先用递归的方法当然比较容易解决: 1 /** 2 * Definition

对称树 Symmetric Tree

算法能力一直不好,决定利用晚上和假期时间多做点算法题,动动手,提高自己.大家看到我的水平低还望莫要嘲笑,嘻嘻. 这一题呢是LeetCode上的对称树问题, 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 fol

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

【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

leetcode || 101、Symmetric Tree

problem: 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

【LeetCode】Symmetric Tree 判断一棵树是否是镜像的

题目:Symmetric Tree <span style="font-size:18px;">/**LeetCode Symmetric Tree 对称的树 * 思路:判断一棵树是否对称,1.有左子树就要有右子树 * 2.除根节点外对称节点值要相同 * 注意:对称后就是左子树的左节点和右子树的右节点比较 * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; *

Leetcode:Symmetric Tree 判断对称树

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 解题分析: 二叉树递归,始终是第一颗二叉树的左子树和第二颗二叉树的右

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]

leetcode 之 Symmetric Tree 镜像树

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 递归解法:由于对称性,每次把一个节点的左子树和它兄弟节点的左子树进行