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. ...
随机推荐
- spoj 665
直接判 没什么算法 也没什么技巧 水水~~ #include <cstdio> #include <cstring> #include <algorithm> ...
- uva 10069
简单的dp 但是一个大数加法 套用了末位大牛的类模板 #include <cstdio> #include <cstring> #include <algorithm& ...
- ICMP and InetAddress.isReachable()
In Java it is only possible to work with two types of sockets: stream based ones (or TCP ones - java ...
- JavaWeb学习总结(四十九)——简单模拟Sping MVC
在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...
- adb开启不了解决方案
原文地址: adb开启不了解决方案 - vaecer - 博客频道 - CSDN.NET http://blog.csdn.net/vaecer/article/details/45894643 ...
- 172. Factorial Trailing Zeroes
题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- WCF实例上下文
实例上下文模式(IntanceContext Mode)表示服务端的服务实例与客户端的服务代理的绑定方式. 在WCF中有三种不同的实例上下文模式,单调(Per-Call)模式,会话(Per-Sessi ...
- js团购倒计时
客户端代码可以看: http://www.zhangxinxu.com/wordpress/2010/07/%E5%9B%A2%E8%B4%AD%E7%B1%BB%E7%BD%91%E7%AB%99% ...
- html元素elem.style.top.left始终为空
有如下元素: <div id="div1" >div1</div> #div1{ width:100px; height:100px; position ...
- 如何解决MySQLAdministrator 启动报错
运行环境:MySQL 5.1.41 win32 ZIP 非安装版MySQL GUI Tools 5.0(版本1.2.17.0) 运行MySQLAdministrator时提示:服务器服务或配置文件不能 ...