Leetcode 104: Maximum Depth of Binary Tree
程序员文章站
2024-02-12 22:41:28
...
问题描述
java实现
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
//way1
class Solution {
//递归 分治的思想
public int maxDepth(TreeNode root) {
int depth=0;
if(root==null)
return depth;
//分
int depthLeft=0;
int depthRight=0;
if(root.left!=null)
depthLeft=maxDepth(root.left);
if(root.right!=null)
depthRight=maxDepth(root.right);
//合
return Math.max(depthLeft,depthRight)+1;
}
}
// //way2
// class Solution {
// //递归 遍历的思想
// private int depth;
// public int maxDepth(TreeNode root) {
// depth = 0;
// int depthLeft = 0;
// helper(root,1);
// return depth;
// }
// private void helper(TreeNode node,int currentDepth){
// if(node==null)
// return;
// if(currentDepth>depth)
// depth=currentDepth;
// helper(node.left,currentDepth+1);
// helper(node.right,currentDepth+1);
// }
// }
上一篇: Unity Shader镜面效果
下一篇: 微信小程序使用视频播放器video组件
推荐阅读
-
LeetCode------Maximum Depth of Binary Tree
-
Leetcode 104: Maximum Depth of Binary Tree
-
leetcode Maximum Width of Binary Tree
-
LeetCode-297.Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)
-
【LeetCode】104. Maximum Depth of Binary Tree 二叉树的深度 DFS BFS 递归方式 迭代方式 JAVA
-
leetcode笔记:Invert Binary Tree
-
minimum-depth-of-binary-tree
-
【leetcode】-700. Search in a Binary Search Tree 查找二叉搜索树
-
Leetcode——108. Convert Sorted Array to Binary Search Tree
-
Leetcode 108. Convert Sorted Array to Binary Search Tree