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. Feel Good(两遍单调栈维护区间+前缀和)

    Bill is developing a new mathematical theory for human emotions. His recent investigations are dedic ...

  2. P3345 [ZJOI2015]幻想乡战略游戏

    传送门 考虑先随便找一个点作为根,然后再慢慢移动根,这样一步步走到最优的点 设 $sum[x]$ 表示节点 $x$ 的子树的军队数,$len(x,y)$ 表示 $x,y$ 之间边的长度 那么对于根节点 ...

  3. WebApi接入Swagger

    1.新建webApi项目 2.nuget引入 swagger 3.在项目属性里配置输出 xml文件 4.打开SwaggerConfig.cs编辑 protected static string Get ...

  4. oracle 备份恢复篇(二)---rman 增备恢复--不完全恢复

    一,环境准备 全备脚本: export TMP=/tmp export TMPDIR=$TMP export ORACLE_BASE=/u01 export ORACLE_SID=prod expor ...

  5. js 判断各种数据类型 typeof 几种类型值

    了解js的都知道, 有个typeof  用来判断各种数据类型,有两种写法:typeof   xxx   ,typeof(xxx) 如下实例: typeof   2      输出   number   ...

  6. (转)linux sed命令就是这么简单

    sed替换命令 原文:https://www.cnblogs.com/zd520pyx1314/p/6061337.html http://www.cnblogs.com/wangqiguo/p/67 ...

  7. 022-pinyin4j工具类模板

    模板一 package ${enclosing_package}; import java.util.Arrays; import net.sourceforge.pinyin4j.PinyinHel ...

  8. Linux下Makefile的automake生成全攻略--转

    http://www.yesky.com/120/1865620.shtml 作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便.一般情况下 ...

  9. centos6.5下搭建oracle 11g

    1.安装依赖 sudo yum install binutils compat-libstdc++-33 compat-libstdc++-33.i686 elfutils-libelf elfuti ...

  10. SVM之Python实现

    SVM Python实现 Python实现SVM的理论知识 SVM原始最优化问题: \[ min_{w,b,\xi}{1\over{2}}{||w||}^2 + C\sum_{i=1}^m\xi^{( ...