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. js多行省略

    $(function (){ // var $introduce = $(".c-introduce").html(); // $new_introduce = $introduc ...

  2. 学习PYTHON之路, DAY 6 - PYTHON 基础 6 (模块)

    一 安装,导入模块 安装: pip3 install 模块名称 导入: import module from module.xx.xx import xx from module.xx.xx impo ...

  3. PHP xml 转换为 array

    retrun json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), tru ...

  4. java selenium (十) 操作浏览器

    本篇文章介绍selenium 操作浏览器 阅读目录 浏览器最大化 前进,后退, 刷新 public static void testBrowser(WebDriver driver) throws E ...

  5. Dom4j解析xml

    public class Dom4jTest { // Dom4j解析xml // 因为所有的方法都需要Dom树 static Document document = null; static Ele ...

  6. 四步完成NodeJS安装,配置和测试

    四步完成NodeJS安装,配置和测试 NodeJS 官网地址: http://nodejs.org/ 第一步:在官网点击 ’ INSTALL ’,下载相应的版本(我的机器是Win7专业版 64bit) ...

  7. js关闭子窗口,刷新父窗口

    父页面js:function btnAdd_onclick() {window.open("xxx.jsp", "","height=600, wid ...

  8. 一次DB服务器性能低下引发的对Nonpaged Pool Leak问题的诊断

    1. 问题表象+分析 最开始是DB访问性能下降,某个不用Cache.直接到DB的查询10s+都不返回.上去一看,DB Server内存97%,可用内存才100多M. Windows毕竟不是iOS,不留 ...

  9. codeforces 501 C,D,E

    C题意: 给定n个点(标号0~n-1)的度数(就是与其邻接的点的个数)和所有与它邻接的点标号的异或和,求满足这些条件的树的边应该是怎么连的,将边输出出来 这里可以理解成拓扑排序的方式考虑,当i度数为1 ...

  10. C# ADO.NET 连接Sybase 数据库

    using Sybase.Data.AseClient;//反编译修改后的DLL public class SybaseHelper { public AseConnection con; publi ...