Problem Link:

https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/

Simply BFS from root and count the number of levels. The code is as follows.

# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return an integer
def maxDepth(self, root):
"""
Use BFS from root, and count the steps
"""
if not root:
return 0
count = 0
q = [root]
while q:
count += 1
new_q = []
for node in q:
if node.left:
new_q.append(node.left)
if node.right:
new_q.append(node.right)
q = new_q
return count

【LeetCode OJ】Maximum Depth of Binary Tree的更多相关文章

  1. 【LeetCode练习题】Maximum Depth of Binary Tree

    Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...

  2. 【LeetCode OJ】Minimum Depth of Binary Tree

    Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...

  3. 【leetcode❤python】 Maximum Depth of Binary Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  4. 【LeetCode练习题】Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  5. LeetCode OJ 104. Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. LeetCode OJ:Maximum Depth of Binary Tree(二叉树最大深度)

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  7. 【Leetcode】【Easy】Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  8. 【leetcode刷题笔记】Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  9. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

随机推荐

  1. 基于任务的异步模式(TAP)

    Task .net 4.0为我们带来了Task的异步,我们有以下三种方法创建Task. 1,Task.Factory.StartNew,比较常用. 2,Task.Run,是.net 4.5中增加的. ...

  2. easyui datagrid 逻辑分页

    function getGroupUsers(groupPath) { $('#tbGroupUsersList').datagrid({ width: 800, height: 100, nowra ...

  3. 判断Sql Server2008中ntext不为空

    select * from 表名 where datalength(列名)=0 or datalength(列名) is null

  4. windows C input 注意

    windows控制台输入,默认是以文本模式打开,即使重定向输入,文本模式不变,所以输入时无法读到cr,因为windows已经把crlf转换成单个lf. 如果freopen("CON" ...

  5. CentOS6.4下基于Nginx搭建mp4/flv流媒体服务器

    我的步骤如下:1. 安装依赖包: yum install glibc.i686#yum –y update#yum -y install gcc glibc glibc-devel make nasm ...

  6. Install Atom editor in ubuntu 14.04

    Step 1: Add repository sudo add-apt-repository ppa:webupd8team/atom Step 2: Update the repository su ...

  7. java解析XML文件

    dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点,同时它也是一个开放源 ...

  8. 【工具】【电子设计】超屌的 fritzing 新建元件

    fritzing 有多好,用了才知道,但是通常会遇到一个问题,他的元件库不一定够用,这时候就得自己做元件了,但是搜了一下网上没有相关的教程啊. 算了,去官网看英文吧.. 首先在最新版本不支持直接新建元 ...

  9. Visual Studio安装及单元测试

    一.安装环境 操作系统版本:Win10中文版64位 CPU:i5-4200M  2.50GHz 硬盘内存:500G 二.软件版本 Visual Studio 2013 三.安装过程 1.首先开始安装, ...

  10. 什么是Alpha通道?

    图像处理(Alpha通道,RGB,...)祁连山(Adobe 系列教程)****的UI课程 一个也许很傻的问题,在图像处理中alpha到底是什么?  Alpha通道是计算机图形学中的术语,指的是特别的 ...