LeetCode(剑指 Offer)- 55 - I. 二叉树的深度

简介: LeetCode(剑指 Offer)- 55 - I. 二叉树的深度

题目链接:点击打开链接

题目大意:

解题思路:

相关企业

  • 领英(LinkedIn)
  • 字节跳动
  • Facebook
  • 亚马逊(Amazon)
  • 谷歌(Google)
  • 微软(Microsoft)
  • 彭博(Bloomberg)
  • 苹果(Apple)

AC 代码

  • Java
/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/// 解决方案(1)classSolution {
intres=0;
publicintmaxDepth(TreeNoderoot) {
dfs(root, 1);
returnres;
    }
voiddfs(TreeNodenode, intl) {
if (node==null) {
return;
        }
res=Math.max(res, l);
dfs(node.left, l+1);
dfs(node.right, l+1);
    }
}
// 解决方案(2)classSolution {
publicintmaxDepth(TreeNoderoot) {
if(root==null) return0;
returnMath.max(maxDepth(root.left), maxDepth(root.right)) +1;
    }
}
// 解决方案(3)classSolution {
publicintmaxDepth(TreeNoderoot) {
if(root==null) return0;
List<TreeNode>queue=newLinkedList<>() {{ add(root); }}, tmp;
intres=0;
while(!queue.isEmpty()) {
tmp=newLinkedList<>();
for(TreeNodenode : queue) {
if(node.left!=null) tmp.add(node.left);
if(node.right!=null) tmp.add(node.right);
            }
queue=tmp;
res++;
        }
returnres;
    }
}
  • C++
/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/// 解决方案(1)classSolution {
public:
intmaxDepth(TreeNode*root) {
if(root==nullptr) return0;
returnmax(maxDepth(root->left), maxDepth(root->right)) +1;
    }
};
// 解决方案(2)classSolution {
public:
intmaxDepth(TreeNode*root) {
if(root==nullptr) return0;
vector<TreeNode*>que;
que.push_back(root);
intres=0;
while(!que.empty()) {
vector<TreeNode*>tmp;
for(TreeNode*node : que) {
if(node->left!=nullptr) tmp.push_back(node->left);
if(node->right!=nullptr) tmp.push_back(node->right);
            }
que=tmp;
res++;
        }
returnres;
    }
};
目录
相关文章
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
44 6
|
2月前
|
存储 算法
二叉树进阶-学会层序遍历助你一次刷完leetcode10道题
文章深入探讨了二叉树的层序遍历方法,并展示了如何通过队列实现层序遍历的算法逻辑,同时指出掌握层序遍历技巧可以帮助解决LeetCode上的多道相关题目。
二叉树进阶-学会层序遍历助你一次刷完leetcode10道题
|
2月前
|
算法 Java
LeetCode第94题二叉树的中序遍历
文章介绍了LeetCode第94题"二叉树的中序遍历"的解法,使用递归实现了中序遍历的过程,遵循了"左根右"的遍历顺序,并提供了清晰的Java代码实现。
LeetCode第94题二叉树的中序遍历
|
2月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
38 7
|
2月前
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
17 3
|
2月前
|
存储 算法 Java
LeetCode经典算法题:二叉树遍历(递归遍历+迭代遍历+层序遍历)以及线索二叉树java详解
LeetCode经典算法题:二叉树遍历(递归遍历+迭代遍历+层序遍历)以及线索二叉树java详解
64 0
|
2月前
|
算法 Java
LeetCode初级算法题:子数组最大平均数+二叉树的最小深度+最长连续递增序列+柠檬水找零
LeetCode初级算法题:子数组最大平均数+二叉树的最小深度+最长连续递增序列+柠檬水找零
33 0
|
5天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
38 4
|
2月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
82 2