leetcode—Same Tree
1.题目描述
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
2.解法分析
与上一篇文章“Symmetric Tree”一样的思路,只是要证明两棵树完全一样,需对两棵树进行一模一样的遍历。当然最好是深度搜索。
/*** Definition for binary tree* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/class Solution {public:bool isSameTree(TreeNode *p, TreeNode *q) {// Start typing your C/C++ solution below// DO NOT write int main() functionvector<TreeNode *> vp;vector<TreeNode *> vq;TreeNode *curp= p;TreeNode *curq =q;while(curp||!vp.empty()){while(curp){if(!curq)return false;if(curp->val!=curq->val)return false;vp.push_back(curp);vq.push_back(curq);curp=curp->left;curq=curq->left;}if(!vp.empty()){if(curp)return false;curp=vp.back();vp.pop_back();curp=curp->right;curq=vq.back();vq.pop_back();curq=curq->right;}}if((vp.empty()&&!vq.empty())||(!vp.empty()&&vq.empty()))return false;if((curp&&!curp)||(!curp&&curq))return false;return true;}};
leetcode—Same Tree的更多相关文章
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- [LeetCode] Binary Tree Upside Down 二叉树的上下颠倒
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
随机推荐
- 关于ax+by=c的解x,y的min(|x|+|y|)值问题
首先我们移动一下项,并强行让a>b. 然后我们可以画出这样一个图像 我们发现,在线段l与x轴交点处的下方,x,y的绝度值是递增的,所以我们不考虑那个最小点在下端. 之后我们发现在点的上端,因为斜 ...
- hdu 4294 Multiple
思路: 首先给出一个结论,就是最多用两个数就可以表示任何数的倍数. 证明 :对于一个数字a,可以构造出的数字有 a,aa,aaa,aaaa,aaaaa,…… 每一个数对于n都有一个余数,余数最多有n个 ...
- highcharts 切换
<!doctype html> <html lang="en"> <head> <script type="text/javas ...
- QT的QWidget和Delphi的TPanel很像,都是万能的基础控件
都只提供了最基本的功能,实际可以在上面随心所欲的创造新的控件.而自身也已经拥有基础的显示功能,而TCustomControl就不行. 比如,这样使用QWidget,直接就可以显示: void Main ...
- Hadoop常用命令汇总
启动Hadoop 进入HADOOP_HOME目录. 执行sh bin/start-all.sh 关闭Hadoop 进入HADOOP_HOME目录. 执行sh bin/stop-all.sh 1.查看指 ...
- sizeof(数组)
这里就不讨论一般的数组长度计算了,只说明一下任何数据到了函数的形参中都将退化为指针,所以计算大小的时候,也是计算的指针的大小 直接上代码了 // class sizeof测试.cpp : 定义控制台应 ...
- Spring-Data-JPA学习
Spring-Data-JPA结构图 网址: http://blog.sina.com.cn/s/blog_667ac0360102ecsf.html
- 在SQLite中使用索引优化查询速度
在进行多个表联合查询的时候,使用索引可以显著的提高速度,刚才用SQLite做了一下测试. 建立三个表: create table t1 (id integer primary key,num inte ...
- poj12月其他题解(未完)
最近编程的时间比较少啊…… poj3253 就是个合并果子,各种优先队列即可(显然单调队列最优) poj3263 线段树统计每个点被覆盖了多少次即可,注意要去重 poj3625 最小生成树 poj36 ...
- HDU Senior's Gun (水题)
题意: 给n把枪,m个怪兽,每把枪可消灭1怪兽,并获得能量=枪的攻击力-怪兽的防御力.求如何射杀能获得最多能量?(不必杀光) 思路: 用最大攻击力的枪杀防御力最小的怪兽明显可获得最大能量.如果每把枪都 ...