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.

递归的解法:

新建一个递归调用自身的函数,输入是待比较的左结点和右结点,输出是对称判定结果:true or false;

 class Solution {
public:
bool isSymmetric(TreeNode *root) {
if (!root)
return true; return isSame(root->left, root->right); } bool isSame(TreeNode *node_l, TreeNode *node_r){
if (!node_l && !node_r)
return true; if (!node_l || !node_r)
return false; if (node_l->val != node_r->val){
return false;
} return isSame(node_l->left, node_r->right) && \
    isSame(node_l->right, node_r->left);
} };

迭代的解法:

新建两个栈用于存放待比较的左子树结点和右子树结点。每次迭代拿出两个栈中同位置元素进行比较,结束后删除此比较过的元素,并将其左右儿子压栈待比较。(还有只用一个栈的方法,并没有节省空间,略)

 class Solution {
public:
bool isSymmetric(TreeNode *root) {
if (!root)
return true; stack<TreeNode *> leftNodeStack;
stack<TreeNode *> rightNodeStack;
leftNodeStack.push(root->left);
rightNodeStack.push(root->right);
TreeNode *leftNode;
TreeNode *rightNode; while (!leftNodeStack.empty()) {
leftNode = leftNodeStack.top();
rightNode = rightNodeStack.top();
leftNodeStack.pop();
rightNodeStack.pop(); if (!leftNode && !rightNode) {
continue;
} if ((!leftNode && rightNode) || \
(leftNode && !rightNode)) {
return false;
} if (leftNode->val != rightNode->val) {
return false;
} leftNodeStack.push(leftNode->left);
leftNodeStack.push(leftNode->right);
rightNodeStack.push(rightNode->right); // Notice
rightNodeStack.push(rightNode->left);
}
return true;
}
};

【Leetcode】【Easy】Symmetric Tree的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【leetcode刷题笔记】Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解 ...

  6. 【leetcode刷题笔记】Convert Sorted List to Binary Search Tree

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  7. 【leetcode刷题笔记】Same Tree

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

  8. 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  9. 【leetcode刷题笔记】Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  10. 【leetcode刷题笔记】Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

随机推荐

  1. Oracle笔记-Multitable INSERT 的用法

    [转自]  http://blog.chinaunix.net/uid-8504518-id-3310531.html 为避免日趋衰退的记忆力,参考官方E文文档<Introduction to ...

  2. dcoker machine

    Docker Machine是一个安装和管理 Docker 的工具, 它有自己的命令行工具:docker-machine.Docker Machine简化了Docker的安装和远程管理, 不仅可以管理 ...

  3. Python入门笔记——(1)数字与表达式

    一.算术运算 整除:// 取余:% 乘方:** (a ** b = pow(a, b)) 十六进制表示:0x...,八进制表示0... round(x [, n]):对x从小数点第n位取四舍五入结果, ...

  4. shell 函数与内置变量

    1,特殊shell变量 $# 传递到脚本的参数个数 $* 以一个单字符串显示所有向脚本传递的参数 $$ 脚本运行的当前进程ID号 $! 后台运行的最后一个进程的ID号 $@ 与$*相同,但是使用时加引 ...

  5. pytorch 安装

    安装pytorch时,官网不能选择版本.原以为是浏览器问题,换了几个浏览器都不行. 后来FQ之后,就能选择版本了. sudo pip install torch torchvision

  6. Docker的镜像迁移

    [root@localhost ~]# mkdir /opt/soft/ [root@localhost ~]# docker save c3987965c15d > /opt/soft/pos ...

  7. 牛客网Java刷题知识点之为什么HashMap不支持线程的同步,不是线程安全的?如何实现HashMap的同步?

    不多说,直接上干货! 这篇我是从整体出发去写的. 牛客网Java刷题知识点之Java 集合框架的构成.集合框架中的迭代器Iterator.集合框架中的集合接口Collection(List和Set). ...

  8. Spark 概念学习系列之从spark架构中透视job(十六)

    本博文的主要内容如下:  1.通过案例观察Spark架构 2.手动绘制Spark内部架构 3.Spark Job的逻辑视图解析 4.Spark Job的物理视图解析 1.通过案例观察Spark架构 s ...

  9. mobile开发技巧(转)

    1.隐藏地址栏 很多文档介绍通过调用 window.scrollTo(0, 1); 就可以隐藏地址栏,但是通过实践发现隐藏地址栏还是真够坑爹的啊,只调用这一句话一般不会起作用,我们需要 functio ...

  10. [Activator- HelloAkka] Define our Messages

    An Actor does not have a public API in terms of methods that you can invoke. Instead its public API ...