【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 ...
随机推荐
- adb自救指南
以下只能自救专用 adb.exe root adb remount adb.exe connect <Ip> adb.exe install [-r] [-d] <apkPath&g ...
- 微信小程序 修改(自定义) 单选/复选按钮样式 checkbox/radio样式自定义
参考文章: 微信小程序 修改(自定义) 单选/复选按钮样式 checkbox/radio样式自定义
- web 页面 验证码 实现
1. 前台页面代码: 页面刷新时会自动请求 ${pageContext.request.contextPath}/yanzheng?yz=&time=-1111 这个action <f ...
- session.flush()与session.clear()的区别及使用环境
[From] http://blog.csdn.net/leidengyan/article/details/7514484 首先session是有一级缓存的,目的是为了减少查询数据库的时间,提高效率 ...
- superobject 设定排序方式
(* * Super Object Toolkit * * Usage allowed under the restrictions of the Lesser GNU General Public ...
- 关于i18n
现在工作主要负责小程序端,很少负责backend.最近的一个任务是配置多语言.因为一开始都是写死的中文,现在需要把那些变成英文. 狂搜了一波,其实网上的方法都不怎好.(可能就是一开始看的时候觉得好.) ...
- easyUI--datagrid 实现按键控制( enter tab 方向键 )
1.表格定义时加上 onClickCell: onClickCell,2.定义列时加入编辑器3.引入 key.js 即可使用 enter 键 或者向下箭头 选中单元格下移 选中单元格上移 tab键 选 ...
- 抽象工厂方法模式(Abstract Factory Pattern)
Provide an interface for creating families of related or dependent objects without specifying their ...
- nodejs基础知识查缺补漏
1. 单线程.异步I/O.对比php nodejs是单线程的,但是是异步I/O,对于高并发时,它也能够快速的处理请求,100万个请求也可以承担,但是缺点是非常的耗内存,但是我们可以加大内存, 所以能用 ...
- word 快捷键
Ctrl+shift+F9 清除word文档中的超链接