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 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 isSymmetric(TreeNode* root) {
13         if(root == NULL)
14             return true;
15         if(!root->left && !root->right)
16             return true;
17         if(root->left && !root->right || !root->left && root->right)
18             return false;
19         return checkSymmetric(root->left, root->right);
20     }
21
22     bool checkSymmetric(TreeNode * left, TreeNode * right)
23     {
24         if(!left && !right)
25             return true;
26         if(!left && right || left && !right)
27             return false;
28         if(left->val != right->val)
29             return false;
30         return checkSymmetric(left->left, right->right) && checkSymmetric(left->right, right->left);
31     }
32 };

题目还要求用非递归的方式来实现,制造两个队列就可以实现了:

 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 isSymmetric(TreeNode* root) {
13         queue<TreeNode *> q1;
14         queue<TreeNode *> q2;
15         if(root == NULL) return true;
16         if(!root->left && !root->right) return true;
17         if(root->left && !root->right || !root->left && root->right) return false;//val
18         q1.push(root->left);
19         q2.push(root->right);
20         TreeNode * tmpLeft, *tmpRight;
21         while(!q1.empty() && !q2.empty()){
22             tmpLeft = q1.front();
23             tmpRight = q2.front();
24             q1.pop(), q2.pop();
25             if(!tmpLeft && !tmpRight)
26                 continue;
27             if(!tmpLeft && tmpRight || tmpLeft && !tmpRight)
28                 return false;
29             if(tmpLeft->val != tmpRight->val)
30                 return false;
31             q1.push(tmpLeft->left);
32             q1.push(tmpLeft->right);
33             q2.push(tmpRight->right);
34             q2.push(tmpRight->left);
35         }
36         return q1.empty() && q2.empty();
37     }
38 };
时间: 2025-01-06 07:16:49

LeetCode OJ:Symmetric Tree Tree(对称的树)的相关文章

[LeetCode OJ] 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 OJ - Symmetric Tree &amp;&amp; Same Tree

这两道题,大同小异. 我都是用BFS,在遍历的过程,判断结构是否相同/对称,值是否相同. 下面是AC代码: 1 /** 2 * Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 3 * @param root 4 * @return 5 */ 6 public boolean isSymmetricRecursively(TreeNode root){ 7

LeetCode OJ - Balanced Binary Tree

判断树是否是平衡的,这道题中的平衡的概念是指任意节点的两个子树的高度相差不超过1,我用递归的方法把所有的节点的高度都计算了下,并且在计算的过程记录每个节点左右两颗子树的高度差,最后通过遍历这个高度差就可以知道是否是平衡的. 下面是AC代码: 1 /** 2 * Given a binary tree, determine if it is height-balanced. 3 * For this problem, a height-balanced binary tree is defined

LeetCode OJ - construct Binary Tree from Inorder and Postorder/Preorder Traversal

不断递归的实现!!!! 下面是AC代码: 1 /** 2 * Given inorder and postorder traversal of a tree, construct the binary tree. 3 * @param inorder 4 * @param postorder 5 * @return 6 */ 7 public TreeNode buildTree(int[] inorder,int[] postorder){ 8 if(inorder == null || po

LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)

1 /** 2 * Definition for binary tree 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 isSymmetric(TreeNode *root) {

LeetCode OJ 107. Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order traver

LeetCode OJ 144. Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? Subscribe to see which companies asked this qu

&lt;LeetCode OJ&gt; 199. Binary Tree Right Side View

Total Accepted: 40438 Total Submissions: 117654 Difficulty: Medium Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example: Given the following binary

LeetCode OJ 100. 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. 很简单,直接上代码: 1 /** 2 * Definition for a binary tree node. 3 * public