[LeetCode] Same Tree
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.
做过symmetric tree再做这题就简单了,同时深搜两颗树,同时对比他们的左孩子和右孩子就可以了。
bool isSameTree(TreeNode *p, TreeNode *q) {
if (!p && !q) return true;
if (!(p && q)) return false;
if (p->val == q->val) {
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
return false;
}
[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. ...
随机推荐
- 1 python学习——python环境配置
1 python学习--python环境配置 要学习python语言,光看书看教程还是不好,得动手去写.当然,不管学习什么编程语言,最佳的方式还在于实践. 要实践,先得有一个Python解释器来解释执 ...
- struts2和hibernate整合的小Demo
jar包下载地址 创建一个web项目. 导入jar包 配置web.xml <?xml version="1.0" encoding="UTF-8"?> ...
- phpcms导航中添加内部链接
phpcms中栏目有3中类型 1.普通栏目 2.单网页 3.外部链接 其中如果想添加本站的内部链接,可以使用3,然后在添加链接的地方填入剩下的地址即可(需要以/开头) 如: /index.php?m= ...
- EOS/普元:概述:中国IT业的悲哀
公司引入了普元的EOS作为公司的基础架构平台,今后的所有项目将逐步向EOS的迁移,但对EOS的研究又让我不得不说出以下话: 1.EOS确实够简单,但未免简单过了头:从语言层面看EOS 因为EOS将成为 ...
- js日期、月份:日期加一天等
// 日期,在原有日期基础上,增加days天数,默认增加1天 function addDate(date, days) { if (days == undefined || days == '') { ...
- sql server 2008 不允许保存更改,您所做的更改要求删除并重新创建以下表 的解决办法
启动SQL Server 2008 Management Studio 工具菜单----选项----Designers(设计器)----阻止保存要求重新创建表的更改 取消勾选即可.
- C++ STL 的实现:
C++ STL 的实现: 1.vector 底层数据结构为数组 ,支持快速随机访问 2.list 底层数据结构为双向链表,支持快速增删 3.deque 底层数据结构为一个中央控制器和多个缓 ...
- Java for LeetCode 219 Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i ...
- ABAP 行列稳定刷新语句
DATA stbl TYPE lvc_s_stbl. stbl-row = 'X'." 基于行的稳定刷新 stbl-col = 'X'." 基于列稳定刷新 ...
- AUTOSSH,ssh反向代理
在本地机器 1)ssh-keygen 2)ls ~/.ssh/ 应该有三个文件 id_rsa id_rsa.pub known_hosts 拷贝id_rsa.pub到远程服务器,然后在 ...