LeetCode 104. Maximum Depth of Binary Tree
来源:互联网 发布:mac 207 编辑:程序博客网 时间:2024/05/17 02:30
题目
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
public class Solution104 { public static void main(String[] args) { Solution104 solution104 = new Solution104(); TreeNode treeNode0 = new TreeNode(1); TreeNode treeNode1 = new TreeNode(1); TreeNode treeNode2 = new TreeNode(1); TreeNode treeNode3 = new TreeNode(1); TreeNode treeNode4 = new TreeNode(1); treeNode0.left = treeNode1; treeNode0.right = treeNode2; treeNode2.left = treeNode3; treeNode2.right = treeNode4; int depth = solution104.maxDepth(treeNode0); System.out.print(depth); } /** * 我的做法,用递归实现,但是线上提交显示超时. * 应该修改为注释的样子,用left,right保存 */ public int maxDepth(TreeNode root) { if(root==null){ return 0; } if(1+maxDepth(root.left) > 1+maxDepth(root.right)){ return 1+maxDepth(root.left); }else{ return 1+maxDepth(root.right); }// int left = 1+maxDepth(root.left);// int right= 1+maxDepth(root.right);// if(left > right){// return left;// }else{// return right;// } }}class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}
0 0
- [LeetCode]104.Maximum Depth of Binary Tree
- 【LeetCode】104.Maximum Depth of Binary Tree
- [Leetcode] 104. Maximum Depth of Binary Tree
- LeetCode --- 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- leetCode 104. Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree LeetCode
- 104. Maximum Depth of Binary Tree LeetCode
- LeetCode 104. Maximum Depth of Binary Tree
- leetcode:104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- [LeetCode]104. Maximum Depth of Binary Tree
- leetcode 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- 【LeetCode】104. Maximum Depth of Binary Tree
- LeetCode#104. Maximum Depth of Binary Tree
- Leetcode 104. Maximum Depth of Binary Tree
- LeetCode *** 104. Maximum Depth of Binary Tree
- 高仿QQ源码 界面(3)
- 【Android】图像增强
- Spark Shuffle内幕
- Netty+Zookeeper实现一个类似Dubbo的RPC框架
- 统一编址&独立编址
- LeetCode 104. Maximum Depth of Binary Tree
- MyBatis传入参数与parameterType
- Android自定义圆角对话框
- 自定义View属性的使用
- Sybase ASE 基础(2)创建数据库
- mockito-ArgumentCaptor
- 小米平板刷机教程
- Bmob平台实现头像上传+Volley框架加载图片url
- 【新手入门】C++中的一点问题