[leetcode]104. Maximum Depth of Binary Tree@Java解题报告
来源:互联网 发布:淘宝售后处理规则 编辑:程序博客网 时间:2024/05/16 07:51
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
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.
package go.jacob.day806;import java.util.LinkedList;import java.util.Queue;public class Demo3 {/* * 解法一: * 递归解法,非常简洁 */public int maxDepth(TreeNode root) {if(root==null)return 0;return 1+Math.max(maxDepth(root.left), maxDepth(root.right));}/* * 解法二: 使用queue进行层序遍历 */public int maxDepth_1(TreeNode root) {if (root == null)return 0;Queue<TreeNode> queue = new LinkedList<TreeNode>();int res = 0;queue.add(root);while (!queue.isEmpty()) {int size = queue.size();for (int i = 0; i < size; i++) {TreeNode node = queue.poll();if (node.left != null)queue.add(node.left);if (node.right != null)queue.add(node.right);}res++;}return res;}private class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;}}}
阅读全文
0 0
- [leetcode]104. Maximum Depth of Binary Tree@Java解题报告
- [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 [easy]
- [Leetcode] 104. Maximum Depth of Binary Tree 解题报告
- 【LeetCode】Maximum Depth of Binary Tree 解题报告
- Leetcode 104. Maximum Depth of Binary Tree 二叉树的最大高度 解题报告
- [leetcode]111. Minimum Depth of Binary Tree@Java解题报告
- [Leetcode] Maximum Depth of Binary Tree (Java)
- [LeetCode][Java] Maximum Depth of Binary Tree
- LeetCode|Maximum Depth of Binary Tree-java
- (java)leetcode Maximum Depth of Binary Tree
- leetcode:Maximum Depth of Binary Tree 【Java】
- [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
- 堆排序理解
- Scrapy爬虫框架
- Java中使用Jedis操作Redis
- @ResponseBody修饰String和对象,前台接收的格式
- JAVA第七章集合(四)
- [leetcode]104. Maximum Depth of Binary Tree@Java解题报告
- mysql 在 centos 上安装
- 基于opencv的图像分析,PID算法,串口发送,单片机接收,输出脉冲继而控制步进电机转动
- fast-rcnn 详解
- HDU
- SSH框架搭建(Spring-Strtus-Hibernate)
- C开发lua模块(二) --- 操作table和调用lua函数
- 2017"百度之星"程序设计大赛
- 简单看下Java 的内存及其共享