leetcode二叉树题目总结
leetcode二叉树题目总结
题目链接:https://leetcode-cn.com/leetbook/detail/data-structure-binary-tree/
前序遍历(NLR)
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
preOrder(root, res);
return res;
}
public void preOrder(TreeNode root, List<Integer> res) {
if (root == null)
return;
res.add(root.val);
preOrder(root.left, res);
preOrder(root.right, res);
}
中序遍历(LNR)
inOrder(root.left, res);
res.add(root.val);
inOrder(root.right, res);
后序遍历( LRN )
postOrder(root.left, res);
postOrder(root.right, res);
res.add(root.val);
层次遍历
3
/ \
9 20
/ \
15 7
结果
[
[3],
[9,20],
[15,7]
]
关键代码
public List<List<Integer>> levelOrder(TreeNode root) {
// 空树判断
if(root == null) return result;
// 返回的结果
List<List<Integer>> result = new ArrayList<>();
// 队列
Queue<TreeNode> queue = new LinkedList<>();
// 根节点入队
queue.add(root);
// 队列不为空则循环
while(queue.size()>0){
// 内层节点序列
List<Integer> temp = new ArrayList<>();
// 遍历队列,把当前层的元素从队列取出来,将下一层放入队列
for(int i=0;i<size;i++){
// 当前节点放入结果数组
TreeNode cur = queue.poll();
temp.add(cur);
// 左节点入队
if(cur.left){
queue.add(cur.left);
}
// 右节点入队
if(cur.right){
queue.add(cur.right)
}
}
result.add(temp);
}
return result;
}
二叉树最大深度
递归解决:
递归出口 当前节点 == null 返回0
递归逻辑 Math.max(maxDept(root.left) +1, maxDepth(root.right) +1)
public int maxDepth(TreeNode root) {
if(root == null) return 0;
return Math.max(maxDepth(root.left)+1, maxDepth(root.right)+1);
}
对称二叉树
递归法:
递归出口: 两个节点同时为空 return true 有一个为空 return false
递归逻辑: 返回当前节点值是否相等 && 递归节点1的左节点和节点2的右节点 && 递归节点2的右节点和节点1的左节点
class Solution {
public boolean isSymmetric(TreeNode root) {
return isMirror(root, root);
}
public boolean isMirror(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) return true;
if (t1 == null || t2 == null) return false;
return (t1.val == t2.val)
&& isMirror(t1.left, t2.right)
&& isMirror(t1.right, t2.left);
}
}
路径总和
递归法: 递归出口: 当前节点==null判断 当前的 target是否为0,是则返回true,否返回false
递归逻辑: DFS所有节点,每次遍历到当前节点将target-cur.val
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum){
// 处理root为空的情况
if(root == null){
return false;
}
// 叶子节点判定当前路径是否可行
if(root.left==null && root.right==null){
return root.val == targetSum;
}
return hasPathSum(root.left,targetSum-root.val) || hasPathSum(root.right,targetSum-root.val);
}
}
中序后序构造二叉树
思路:
通过后序遍历确定每一个根节点
通过DFS递归,限定范围得到左右子树
递归出口 后序遍历的head > tail
通过找到当前节点在中序遍历中的位置限定范围
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
int len=inorder.length;
if(len==0)return null;
return dfs(inorder,postorder,0,len-1,0,len-1);
}
TreeNode dfs(int[] inorder, int[] postorder,int head1,int tail1, int head2,int tail2){
// 由于postOrder每次都会在tail-1 所以当只有一个元素则会返回
if(head2>tail2)return null;
int val=postorder[tail2];
TreeNode root=new TreeNode(val);
if(head2==tail2)return root;
int mid=0; //拆分点mid的位置是相对的,因为h1!=h2
while(inorder[head1+mid]!=val)mid++;
root.left=dfs(inorder, postorder, head1, head1+mid-1, head2, head2+mid-1);
root.right=dfs(inorder, postorder, head1+mid+1, tail1, head2+mid, tail2-1);
return root;
}
}
笔记:

前序中序构造二叉树类似
和中序前序类似, 只是每次的限定范围不同
二叉树填充右侧指针
递归法: 限定完美二叉树

class Solution {
public Node connect(Node root){
if(root!=null) dfs(root.left, root.right);
return root;
}
void dfs(Node left, Node right){
if(left ==null || left.next == right) return;
left-> next = right;
dfs(left.left, left.right);
dfs(left.right, right.left);
dfs(right.left, right.right);
}
}
一般情况

/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/
class Solution {
public:
Node* connect(Node* root) {
// 空树判断
if( root == nullptr ) return nullptr;
// 左树为空
if( root -> left != nullptr ) {
if( root -> right != nullptr){
// 最普遍的情况
root -> left -> next = root -> right;
}
else
// 父节点的next的第一个子节点作为next
root -> left -> next = getNext(root -> next);
}
// 右节点不为空
if( root->right != nullptr){
// next
root -> right -> next = getNext( root-> next );
}
// 先判断右节点
connect(root->right);
connect(root->left);
return root;
}
// 通过父节点获取子节点的next结点
Node* getNext(Node* uncle){ if( uncle == nullptr ) return nullptr; if( uncle -> left != nullptr) return uncle->left; if( uncle -> right != nullptr ) return uncle->right; return getNext(uncle->next);
}
};
最近公共祖先
整体思路:1. 通过BFS得到每个节点和其父节点的Map
通过Set,保存p到根节点的路径
在通过获得q到根节点的路径,判断第一个包含在原来的set的节点为最先公共祖先

代码:
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
//记录遍历到的每个节点的父节点。
Map<TreeNode, TreeNode> parent = new HashMap<>();
Queue<TreeNode> queue = new LinkedList<>();
//根节点没有父节点,所以为空
parent.put(root, null);
//队列入队
queue.add(root);
//直到两个节点都找到为止。
while (!parent.containsKey(p) || !parent.containsKey(q)) {
//队列是一边进一边出,这里poll方法是出队,
TreeNode node = queue.poll();
if (node.left != null) {
//左子节点不为空,记录下他的父节点
parent.put(node.left, node);
//左子节点不为空,把它加入到队列中
queue.add(node.left);
}
//右节点同上
if (node.right != null) {
parent.put(node.right, node);
queue.add(node.right);
}
}
Set<TreeNode> ancestors = new HashSet<>();
//记录下p和他的祖先节点,从p节点开始一直到根节点。
while (p != null) {
ancestors.add(p);
p = parent.get(p);
}
//查看p和他的祖先节点是否包含q节点,如果不包含再看是否包含q的父节点……
while (!ancestors.contains(q))
q = parent.get(q);
return q;
}
二叉树的序列化和反序列化
分析思路:

代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root == null) return "#";
Queue<TreeNode> queue = new LinkedList<TreeNode>();
StringBuffer res = new StringBuffer();
queue.add(root);
while(!queue.isEmpty()){
TreeNode node = queue.poll();
if(node == null){
res.append("#,");
continue;
}
res.append(node.val + ",");
queue.add(node.left);
queue.add(node.right);
}
return res.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data == "#") return null;
Queue<TreeNode> queue = new LinkedList<>();
String[] values = data.split(",");
// 第一个节点
TreeNode node = new TreeNode(Integer.parseInt(values[0]));
queue.add(node);
for(int i =1; i < values.length; i++){
TreeNode treeNode = queue.poll();
if(!"#".equals(values[i])){
treeNode.left = new TreeNode(Integer.parseInt(values[i]));
queue.add(treeNode.left);
}
if(!"#".equals(values[++i])){
treeNode.right = new TreeNode(Integer.parseInt(values[i]));
queue.add(treeNode.right);
}
}
return node;
}
}
// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// TreeNode ans = deser.deserialize(ser.serialize(root));
leetcode二叉树题目总结的更多相关文章
- 面试大总结之二:Java搞定面试中的二叉树题目
package BinaryTreeSummary; import java.util.ArrayList; import java.util.Iterator; import java.util.L ...
- leetcode上题目的分类
leetcode链表部分题目 https://zhuanlan.zhihu.com/p/29800285 <[Leetcode][链表]相关题目汇总/分析/总结> leetcode堆部分题 ...
- 二叉树题目集合 python
二叉树是被考察频率非常高的数据结构.二叉树是按照“父节点-左子树&右子树”这样的方式,由根节点不断向下扩展,形成一棵树的结构.二叉树经常被提到的三种遍历方式:前序遍历.中序遍历和后序遍历,既是 ...
- LeetCode二叉树实现
LeetCode二叉树实现 # 定义二叉树 class TreeNode: def __init__(self, x): self.val = x self.left = None self.righ ...
- LeetCode高频题目(100)汇总-Java实现
LeetCode高频题目(100)汇总-Java实现 LeetCode高频题目(100)汇总-Java实现 目录 第01-50题 [Leetcode-easy-1] Two Sum [Le ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- LeetCode SQL题目(第一弹)
LeetCode SQL题目 注意:Leetcode上的SQL编程题都提供了数据表的架构程序,只需要将它贴入本地数据库即可调试自己编写的程序 不管是MS-SQL Server还是MySQL都需要登陆才 ...
- LeetCode 二叉树,两个子节点的最近的公共父节点
LeetCode 二叉树,两个子节点的最近的公共父节点 二叉树 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共父亲节点 https://leetcod ...
- 已知前序(后序)遍历序列和中序遍历序列构建二叉树(Leetcode相关题目)
1.文字描述: 已知一颗二叉树的前序(后序)遍历序列和中序遍历序列,如何构建这棵二叉树? 以前序为例子: 前序遍历序列:ABCDEF 中序遍历序列:CBDAEF 前序遍历先访问根节点,因此前序遍历序列 ...
随机推荐
- 一键智能Mock,你值得拥有
大家好呀,我是一名苦逼的前端开发工程师,为啥苦逼呢,这不,项目下周就要上线了,但是后端还没给我接口,没有接口我就无法调试,工作停滞不前,我也只能坐着干着急. 我报告给了我的老板山哥: 老板,这后端 ...
- Linux常用命令学习笔记——基于CentOS 7
前言:最近在linux培训时复习了一下linux系统中一些常用的命令和用法,整理成了笔记,虽然些许零散,但希望对大家有所帮助. 目录 0.帮助指令 1.关机.重启.注销命令 2.文件和目录操作命令 3 ...
- 事务的隔离级别与MVCC
提到数据库,你多半会联想到事务,进而还可能想起曾经背得滚瓜乱熟的ACID,不知道你有没有想过这个问题,事务有原子性.隔离性.一致性和持久性四大特性,为什么偏偏给隔离性设置了级别? 一切还得从事务说起. ...
- XCTF练习题---MISC---Cephalopod
XCTF练习题---MISC---Cephalopod flag:HITB{95700d8aefdc1648b90a92f3a8460a2c} 解题步骤: 1.观察题目,下载附件 2.拿到手以后发现是 ...
- 3.yum学习笔记
一.yum介绍 将所有的rpm软件包放到指定服务器上,当进行yum在线安装时,可以自动解决依赖性问题. yum配置文件常位于/etc/yum.repo.d 目录下 [root@aaa251 ~]# c ...
- 在Windows2003 server 64位系统上使用ArcEngine开发的WCF服务
之前大篇文章提到,ESRI说AE10.0以后已经不支持WebService的发布,经过一段时间的测试,发现目前10.2.2版本开始的WCF服务都可以正常发布,且运行正常. 先说一下之前遇到的问题,本机 ...
- Spring Boot 2.7.0发布,2.5停止维护,节奏太快了吧
这几天是Spring版本日,很多Spring工件都发布了新版本, Spring Framework 6.0.0 发布了第 4 个里程碑版本,此版本包含所有针对 5.3.20 的修复补丁,以及特定于 6 ...
- 零基础学Java第六节(面向对象二)
本篇文章是<零基础学Java>专栏的第六篇文章,文章采用通俗易懂的文字.图示及代码实战,从零基础开始带大家走上高薪之路! 本文章首发于公众号[编程攻略] 继承 创建一个Person类 我们 ...
- [持续更新] Python学习、使用过程中遇见的非代码层面知识(想不到更好的标题了 T_T)
写在前面: 这篇博文记录的不是python代码.数据结构.算法相关的内容,而是在学习.使用过程中遇见的一些没有技术含量,但有时很令人抓耳挠腮的小东西.比如:python内置库怎么看.python搜索模 ...
- requests入门
1.通过GET请求获得搜索结果的网页源代码 import requests name=input("请输入想要搜索的明星:") url=f'https://www.sogou.co ...