【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 ...
随机推荐
- Java中filter内处理重定向遇到的问题
这是在Java中filter内处理重定向遇到的问题.本意是写一个做URL rewrite 的filter,来重写URL,同时在处理登陆过程中要杀掉当前session,创建新session来代替. 1. ...
- 正确优雅地解决用户退出——JSP及Struts解决方案
摘要 在一个有密码保护的Web应用中,正确处理用户退出过程并不仅仅只需调用HttpSession的invalidate()方法.现在大部分浏览器上都有后退和前进按钮,允许用户后退或前进到一 ...
- C++ GUI Qt4编程(06)-2.3sort
1. 使用Qt设计师创建Sort对话框. 2. sortdialog.cpp /**/ #include "sortdialog.h" SortDialog::SortDialog ...
- Notepad++编译和运行Java
首先要让Notepad++编译和运行Java,前提是电脑里已经配置好了Java的环境(这里可以参考我博客里关于Java环境配置的那篇随笔). 在Notepad++上面的选项栏中找到 插件---> ...
- AIDL(2):服务端回调客户端
1.大致流程 在服务端声明远程服务接口IRemoteService.aidl,并声明回调接口ICallback.aidl 在服务端实现远程服务接口IRemoteService.Stub 使用Remot ...
- 「bzoj1925」「Sdoi2010」地精部落 (计数型dp)
「bzoj1925」「Sdoi2010」地精部落---------------------------------------------------------------------------- ...
- Column 'orders' in order clause is ambiguous
今天报了这个错误 原因是.当使用sql查询语句,使用了join查表.但是这个orders没指定是哪张表的字段 ,发生在自关联情况
- ElasticSearch:集群(Cluster),节点(Node),分片(Shard),Indices(索引),replicas(备份)之间关系
[Cluster]集群,一个ES集群由一个或多个节点(Node)组成,每个集群都有一个cluster name作为标识----------------------------------------- ...
- Spring Security怎样不让默认的ProviderManager清除密码等信息
<authentication-manager erase-credentials="false"> ... </authentication-manager&g ...
- [转]Oracle ROWNUM用法和分页查询总结
本文转自:http://blog.csdn.net/fw0124/article/details/42737671 ****************************************** ...