Leetcode 之Same Tree(48)

用递归比较简单,这里用迭代的方式实现。注意什么时候返回true,什么时候返回false。
bool isSameTree(TreeNode *p, TreeNode *q)
{ stack<TreeNode *> s;
s.push(p);
s.push(q);
while (!s.empty())
{
q = s.top(); s.pop();
p = s.top(); s.pop();
//两个都为空
if (!p && !q)return true;
//一个为空
if (!p || !q)return false;
if (p->val != q->val)return false; s.push(p->left);
s.push(q->left); s.push(p->right);
s.push(q->right);
}
return true;
}
Leetcode 之Same Tree(48)的更多相关文章
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- [LeetCode] 429. N-ary Tree Level Order Traversal_ Easy
Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- LeetCode—— Invert Binary Tree
LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...
- LeetCode:旋转图像【48】
LeetCode:旋转图像[48] 题目描述 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使 ...
随机推荐
- POJ1204:Word Puzzles——题解
http://poj.org/problem?id=1204 题目大意:给一个字母表,求一些字符串的开端第一次出现的位置和字符串的方向(字符串可以按照八个方向放在字母表中可匹配的位置) ——————— ...
- ContestHunter暑假欢乐赛 SRM 15
菜菜给题解,良心出题人!但我还是照常写SRM一句话题解吧... T1经典题正解好像是贪心...我比较蠢写了个DP,不过还跑的挺快的 f[i]=min( f[j-a[j]-1] )+1 { j+a[j ...
- postgresql pgagent 的安装及使用
pgagent 作为postgresql的一个任务调度代理,在postgresql 9.0 以前 是附带在pgadmin 包下面的,只是默认不安装,9.0之后作为了一个单独是的安装包.所以要使用pga ...
- POJ1236:Network of Schools (思维+Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24880 Accepted: 99 ...
- kubernetes创建资源对象yaml文件例子--pod详解
apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Pod #指定创建资源的角色/类型 metadata: #资源的元数据/属性 name: ...
- redis 查看所有键值
zb@zb-computer:/home/wwwroot/default/lion/Admin$ /usr/local/redis/bin/redis-cli 127.0.0.1:6379> k ...
- Python学习笔记(Django篇)——3、创建第一个数据库模型
Django里面集成了SQLite的数据库,对于初期研究来说,可以用这个学习. 第一步,创建数据库就涉及到建表等一系列的工作,在此之前,要先在cmd执行一个命令: python manage.py ...
- 解决oracle数据库 ora-00054:resource busy and acquire with NOWAIT specified 错误
解决oracle数据库 ora-00054:resource busy and acquire with NOWAIT specified 错误 本人在使用pl/sql developer 客户端调用 ...
- asyncio结合线程池
#使用多线程:在协程中集成阻塞io import asyncio from concurrent.futures import ThreadPoolExecutor import socket fro ...
- 上下文管理器 contextlib
from contextlib import contextmanager @contextmanager def tag(name): print "<%s>" % ...