Same Tree - LeetCode
目录
- 题目链接
- 注意点
- 解法
- 小结
题目链接
Same Tree - LeetCode
注意点
- 先判断结点是否为空再访问结点的值
解法
解法一:递归,从根结点开始判断,然后递归判断左右子树。
/*** Definition for a binary tree node.* 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) {if(!p && !q) return true;if((p && !q) || (!p && q) || (p->val != q->val)) return false;return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);}
};
解法二:非递归,和递归思想一样,但是显式的用栈来模拟递归的过程。
class Solution {
public:vector<vector<int> > combine(int n, int k) {vector<vector<int> >ret;vector<vector<int> >vec(1);for(int i = 1; i <= n; i++){int len = vec.size();vector<int> tmp;for(int j = 0; j < len; j++){tmp = vec[j];tmp.push_back(i);if(tmp.size() == k)ret.push_back(tmp);else vec.push_back(tmp);}}return ret;}
};
小结
- 递归的本质就是利用栈实现的
转载于:https://www.cnblogs.com/multhree/p/10500884.html
Same Tree - LeetCode相关推荐
- Convert Sorted Array to Binary Search Tree - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Convert Sorted Array to Binary Search Tree - LeetCode 注意点 不要访问空结点 题目要求的是平衡二叉搜 ...
- Balanced Binary Tree leetcode java
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- Convert Sorted List to Balanced Binary Search Tree leetcode
题目:将非递减有序的链表转化为平衡二叉查找树! 参考的博客:http://blog.csdn.net/worldwindjp/article/details/39722643 利用递归思想:首先找到链 ...
- 208. Implement Trie (Prefix Tree)(Leetcode每日一题-2021.04.14)
Problem A trie (pronounced as "try") or prefix tree is a tree data structure used to effic ...
- LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归
题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...
- LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法
题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...
- LeetCode 98. Validate Binary Search Tree--C++解法--判断是否是BST--递归,迭代做法,中序遍历
LeetCode 98. Validate Binary Search Tree–C++解法–判断是否是BST–递归,迭代做法,中序遍历 LeetCode题解专栏:LeetCode题解 LeetCod ...
- LeetCode 104. Maximum Depth of Binary Tree--二叉树高度--递归或迭代--C++,Python解法
题目地址:Maximum Depth of Binary Tree - LeetCode Given a binary tree, find its maximum depth. The maximu ...
- LeetCode 965 Univalued Binary Tree--判断二叉树的所有节点的值是否相同--python,java解法
题目地址:Univalued Binary Tree - LeetCode Acceptance: 67.6% Difficulty: Easy A binary tree is univalued ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
最新文章
- [转]ghost手动备份及遇见的问题
- 【设计模式】抽象工厂模式 ( 简介 | 适用场景 | 优缺点 | 产品等级结构和产品族 | 代码示例 )
- pytorch运行遇到的问题_如何解决吸塑机在运行中遇到真空度的问题
- Order asynchronous mode
- 高性能Numpy/Scipy加速:使用Intel MKL和Intel Compilers或OpenBLAS(待续)
- 智能合约从入门到精通:完整范例
- java 并发锁_Java并发教程–锁定:内在锁
- 什么样的程序猿,最容易被鄙视?
- 研究员公开Razer 0day,插入鼠标即可获得Windows管理员权限
- python语言源程序文件类型_Python语言源程序文件的文件类型是__________。
- API和Web API(1)
- 手机进程多了,有的进程就无法联网?
- 基于Java的Office 系列文档处理五种工具简单介绍
- Imdisk 虚拟磁盘 » A programmer's site
- 航拍南山区六个文化相关全景VR解读
- 如何免费下载学术论文-谷粉学术
- 安卓系统Remix_OS 的vmware虚拟机安装
- mysql increment_mysql中auto_increment是干什么的?
- DDN获中国工信部—可信区块链评测证书
- matlab fitctree 原理,电力窃漏电用户自动识别.PDF