Algorithms—104.Maximum Depth of Binary Tree
来源:互联网 发布:知乎2016 编辑:程序博客网 时间:2024/05/22 15:06
思路:分左右两路递归查询,查到某个节点下left和right都为null时返回,比较左右的值,取大的
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int maxDepth(TreeNode root) {if (root==null) {return 0;}if(root.left==null&&root.right==null){ return 1;}int left=-1;if(root.left!=null){ left=maxDepth(root.left)+1;}int right=-1;if(root.right!=null){ right=maxDepth(root.right)+1;}if(left==-1){ return right;}if(right==-1){ return left;}return Math.max(left, right);}}
耗时:316ms,测了2次,一次中游,一次勉强上游
0 0
- Algorithms—104.Maximum Depth of Binary Tree
- LeetCode Algorithms #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] 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
- 104. Maximum Depth of Binary Tree
- 104.Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree
- leetCode 104. Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree LeetCode
- 104. Maximum Depth of Binary Tree LeetCode
- 104. Maximum Depth of Binary Tree
- 1012. The Best Rank (25)
- [Leetcode]Basic Calculator II
- Command /usr/bin/codesign failed with exit code 1
- Hibernate 学习经验之谈-(四)关联映射
- “奔跑吧,牛客“---统计一
- Algorithms—104.Maximum Depth of Binary Tree
- 【Mark】黑客与画家 - 第一章 为什么书呆子不受欢迎
- 鬼脚七 两个故事
- STM32 ST-LINK Utility无法下载的处理方法
- 引用形参使用中要注意的问题
- 多核程序设计——缓存一致性协议MESI
- 流程控制和类型转换
- hdu 3496 Watch The Movie
- 数据结构(一)顺序表、链表以及队列