【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 ...
随机推荐
- 洛谷 P3388 【模板】割点(割顶)
题目链接 题解 今天复习了一下割点. 关于\(tarjan\)这里不多讲 \(dfn\)和\(low\)数组的定义想必大家都知道 仔细观察一下,可以发现 假设便利\(u->v\)这条边 如果 \ ...
- POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】
一.题目 Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his ...
- 兼容 火狐、IE 的中a标签用 javascript:void(0); 依然执行跳转的问题
<a onclick="return false;" href="javascript: void(0)" target="_blank&quo ...
- UVALive - 3942 左儿子trie DP
题意:白书P209 本题用普通字典树会更快,为了练习还是尝试再敲一遍左儿子-右兄弟字典树(其实就是字典树上开前向星) dp[i]为满足[i...len)的分配方案数,转移方程为dp[i]=sum{dp ...
- shell 操作环境
一.路径与命令查找顺序 命令的运行程序可以这样看: 1.以相对/绝对的路径执行命令,例“/bin/ls”或“ls” 2.由alias乍到该命令来执行 3.由bash内置的(builtin)命令来执行 ...
- shell 对字符的求长
一,测试环境 echo "To the world you may be one person but to one person you may be the world" 对于 ...
- Zookeeper概念学习系列之zookeeper是什么?
1. Zookeeper是Hadoop的分布式协调服务. 2. 分布式应用程序可以基于它,来实现同步服务,配置维护和命名服务等. 3. zookeeper可以保证数据在zookeeper集群之间的数据 ...
- net.sf.json.JSONException: There is a cycle in the hierarchy! 转json死循环问题解决
解决上述问题遵照两个原则就可以: 1.页面不需要展示关联数据时 解决:将关联对象属性排除掉 2.页面需要展示关联数据时 解决:将关联对象改为立即加载,并且将关联对象中的属性排除
- 在ZYNQ-7000平台上利用PS点亮PL上的LED灯
在ZYNQ-7000平台上利用PS点亮PL上的LED灯 1.实验方案 图1 实验方案系统框图 2.具体步骤 2.1.vivado工程建立 ①打开vivado集成开发环境,点击“Create Pr ...
- 18 Command Line Tools to Monitor Linux Performance
By Ravi Saive Under: Linux Commands, Monitoring Tools On: December 26, 2013 http://www.tecmint.com/c ...