前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode: Symmetric Tree

Leetcode: Symmetric Tree

作者头像
卡尔曼和玻尔兹曼谁曼
发布2019-01-22 17:28:51
3020
发布2019-01-22 17:28:51
举报

题目:

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

代码语言:javascript
复制
    1
   / \
  2   2
 / \ / \
3  4 4  3

But the following is not:

代码语言:javascript
复制
    1
   / \
  2   2
   \   \
   3    3

Note: Bonus points if you could solve it both recursively and iteratively.

思路分析:

题目要求分别用递归和迭代的方法进行。递归这里不说了,直接看代码。迭代算法可以使用C++标准库中的deque数据结构,允许从前面和后面操作元素。

递归算法:

代码语言:javascript
复制
class Solution
{
private:
    bool checkSymmetric(TreeNode *left, TreeNode *right)
    {
    	if (left == NULL && right == NULL)
    	{
    		return true;
    	}
    	else if (left && right && left->val == right->val)
    	{
    		return checkSymmetric(left->left, right->right) && checkSymmetric(left->right, right->left);
    	}
    	else
    	{
    		return false;
    	}
    }
public:
    bool isSymmetric(TreeNode *root)
    {
    	if (root == NULL)
    	{
    		return true;
    	}
    	else
    	{
    		return checkSymmetric(root->left, root->right);
    	}
    }
};

迭代算法(使用deque结构):

代码语言:javascript
复制
class Solution
{
public:
    bool isSymmetric(TreeNode *root)
    {
    	if (!root)
    	{
    		return true;
    	}
    	//如果左右子树都为NULL
    	if (!root->left && !root->right)
    	{
    		return true;
    	}
    	//左右子树一个为NULL一个不为NULL
    	if ((root->left && !root->right) || (!root->left && root->right))
    	{
    		return false;
    	}
    	//左右子树都不为NULL
    	TreeNode *leftNode, *rightNode;
    	deque<TreeNode*> nodeDeque;
		nodeDeque.push_front(root->left);
		nodeDeque.push_back(root->right);
		while (!nodeDeque.empty())
		{
			leftNode = nodeDeque.front();
			rightNode = nodeDeque.back();
			nodeDeque.pop_front();
			nodeDeque.pop_back();

			if (leftNode->val != rightNode->val)
			{
				return false;
			}

			
			if ((leftNode->left && !rightNode->right) || (!leftNode->left && rightNode->right))
			{
				return false;
			}
			if (leftNode->left)
			{
				nodeDeque.push_front(leftNode->left);
				nodeDeque.push_back(rightNode->right);
			}
			
			if ((leftNode->right && !rightNode->left) || (!leftNode->right && rightNode->left))
			{
				return false;
			}
			if (leftNode->right)
			{
				nodeDeque.push_front(leftNode->right);
				nodeDeque.push_back(rightNode->left);
			}
		}
		return true;
	}
};

我看了下,使用递归算法在Leetcode上提交是8ms,使用迭代算法是10ms。

网上有的说采用中序遍历,判断遍历的结果对称就OK了。实际上这是不行的。

代码语言:javascript
复制
class Solution
{
private:
    void inOrder(TreeNode *node, vector<int> &result)
    {
    	if (node)
    	{
    	    inOrder(node->left, result);
    		result.push_back(node->val);
    		inOrder(node->right, result);
    	}
    }
public:
    bool isSymmetric(TreeNode *root)
    {
        if (root == NULL)
        {
            return true;
        }
    	vector<int> result;
    	inOrder(root, result);
    	vector<int>::size_type size = result.size();
    	bool flag = true;
    	for (int i = 0, j = size - 1; i < j; i++, j--)
    	{
    		if (result[i] != result[j])
    		{
    			flag = false;
    			break;
    		}
    	}
    	return flag;
    }
};

这样的程序在比如{1, 2, 3, 3,#, 2}这样的测试用例就不能通过。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年03月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档