leetCode(15):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.

首先递归方法:

bool isChildSymmectric(TreeNode* node1,TreeNode* node2)
{
	if(node1==NULL && node2==NULL)
		return true;
	if(node1==NULL || node2==NULL || node1->val!=node2->val)
		return false;
	return isChildSymmectric(node1->left,node2->right) && isChildSymmectric(node1->right,node2->left);
}

bool isSymmetric(TreeNode* root)
{
	if(root==NULL)
		return true;
	return isChildSymmectric(root->left,root->right);
}

循环方法:把左右子树依次压入到容器中,并一层一层地判断,如果不平衡则立即返回;直至没有新的结点压入到容器中。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root==NULL)
    		return true;
    	vector<TreeNode*> nodes;
    	nodes.push_back(root);
    	int nodesBegin=0;
    	int nodesEnd=nodes.size();	

    	while(nodesBegin<nodesEnd)
    	{
    		int b=nodesBegin;
    		int e=nodes.size()-1;
    		while(b<e)
    		{
    			if(nodes[b]==NULL && nodes[e]==NULL)
    			{
    				b++;
    				e--;
    				continue;
    			}
    			if((nodes[b]==NULL && nodes[e]) || (nodes[b] && nodes[e]==NULL)
    				|| (nodes[b]->val!=nodes[e]->val))
    				return false;

    			nodes.push_back(nodes[b]->left);
    			nodes.push_back(nodes[b]->right);

    			b++;
    			e--;
    		}
    		while(b<nodesEnd)
    		{
    			if(NULL==nodes[b])
    			{
    				b++;
    				continue;
    			}
    			nodes.push_back(nodes[b]->left);
    			nodes.push_back(nodes[b]->right);
    			b++;
    		}
    		nodesBegin=nodesEnd;
    		nodesEnd=nodes.size();
    	}
    	return true;
    }
};
时间: 2024-11-12 21:27:06

leetCode(15):Symmetric Tree的相关文章

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] 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:Symmetric Tree - 判断二叉树是否对称

1.题目名称 Symmetric Tree(判断二叉树是否对称) 2.题目地址 https://leetcode.com/problems/symmetric-tree/ 3.题目内容 英文:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 中文:给定一颗二叉树,检查它是否与自己的镜像是同一棵树(即围绕根节点对称). 4.解题方法 本题与题目"Same Tr

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 题解]: 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][JavaScript]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 https://leetcode.com/problems/symm

【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 sol

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】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; *