【Leetcode】【Easy】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.
递归的解法:
新建一个递归调用自身的函数,输入是待比较的左结点和右结点,输出是对称判定结果: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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【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. 题解 ...
- 【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 ...
- 【leetcode刷题笔记】Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 【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, ...
- 【leetcode刷题笔记】Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 【leetcode刷题笔记】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
随机推荐
- HDU 6336 (规律 + 二维矩阵的前缀和妙用)
题目 给出长度为n 的A矩阵 , 按 int cursor = 0; for (int i = 0; ; ++i) { for (int j = 0; j <= i; ++j) { M[j][i ...
- HDU 6299 Balanced Sequence(贪心)
题目:给出N个只有左右括号字符串 ,这N个字符串的排列顺序是任意的 , 问按最优的排序后 , 得到最多匹配的括号个数 分析: 我们很容易的想到 字符串)()()(( , 这样的字符串可以精简为)(( ...
- java的Spring学习2- junit
1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- hdu1395 2^x mod n = 1(欧拉函数)
2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- centos7.1部署java环境服务器
1.检查操作系统自带java是jdk还是jre(否有javac,本例中没有javac) [root@bogon ~]# ls -l /usr/lib/jvm/总用量 0drwxr-xr-x. 3 ro ...
- Mysql5.7不区分大小写设置
1.编辑mysql配置文件my.cnf 2.在[mysqld]下添加一行 lower_case_table_names=1#0表示区分大小写,1表示不区分大小写
- 集成 Jenkins 和 TestNG 实现自助式自动化测试平台
背景介绍 在软件业十分成熟的今天,敏捷(Agile)开发在业界日益流行,而面临的挑战也日益增多,不断变化的用户需求.缩短的开发周期.频繁的部署上线.复杂的产品架构和团队组织,如何继续保证软件的质量是一 ...
- 单元测试框架AndroidTestCase
我不是讲怎么成为一个安卓测试员,就不写那么多了 就写我们常用的, AndroidTestCase 为一Android平台下通用的测试类,它支持所有JUnit的Assert方法和标准的setUp 和te ...
- 简介SWT Jface
可以使用标准窗口小部件工具箱(Standard Widget Toolkit,SWT)和 JFace 库来开发用于 Eclipse 环境的图形用户界面,而且还可以将它们用于开发单独的 GUI 本机应用 ...
- 深入理解jQuery插件开发【转】
如果你看到这篇文章,我确信你毫无疑问会认为jQuery是一个使用简便的库.jQuery可能使用起来很简单,但是它仍然有一些奇怪的地方,对它基本功能和概念不熟悉的人可能会难以掌握.但是不用担心,我下面已 ...