【LeetCode OJ】Maximum Depth of Binary Tree
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的更多相关文章
- 【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 ...
- 【LeetCode OJ】Minimum Depth of Binary Tree
Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...
- 【leetcode❤python】 Maximum Depth of Binary Tree
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 【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 ...
- 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 ...
- 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 ...
- 【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 ...
- 【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 ...
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
随机推荐
- JavaScipt 样式操作
我们知道HTML样式定义的三种方式: <link/>外部引入也就是定义 CSS 中的 <style/>嵌入式样式 style特性地定义 给一个HTML元素设置css属性,如: ...
- jQuery刷新包含的<jsp:include>页面
jQuery刷新包含页面 JQuery刷新包含页面,以下两种形式均可: <%@include file="../include/header.jsp" %> < ...
- CentOS下 MySQL5.7 详细的部署安装流程
MySQL5.7.14安装过程: 下载5.7版本:wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.14-linux-glibc2 ...
- HEAD FIRST HTML & CSS学习笔记
CSS部分 1. border-bottom属性控制元素下边框的外观. eg: border-bottom:1px solid maroon; P265 下划线 text-decoration: ...
- SAX和DOM解析的区别
XML和JSon是ios解析文件的两种形式, 两种方法各有千秋. 1>. XML分为SAX和DOM两种方式 SAX是按顺序逐行读取文件, 查找到符合条件的内容时就会停止, 而DOM是讲内容一次性 ...
- maven+springmvc+spring+mybatis+velocity整合
一.ssmm简介 ssmm是当下企业最常用的开发框架架构 maven:管理项目jar包,构建项目 spring:IOC容器,事务管理 springmvc:mvc框架 myBatis:持久层框架 v ...
- C#/ASP.NET MVC微信公众号接口开发之从零开发(四) 微信自定义菜单(附源码)
C#/ASP.NET MVC微信接口开发文章目录: 1.C#/ASP.NET MVC微信公众号接口开发之从零开发(一) 接入微信公众平台 2.C#/ASP.NET MVC微信公众号接口开发之从零开发( ...
- 伪静态重写模块rewrite.dll及httpd.ini文件参考下载
伪静态重写模块rewrite.dll及httpd.ini文件参考下载 http://www.ledaokj.com/download/rewrite.rar 服务器端开启伪静态,可以查看以下文章< ...
- TRANSPOSE的DATA步实现
data a; input name $ a b ; cards; x x x y y y ; run; %macro transpose; proc sql noprint ; select cou ...
- 解决远程连接mysql错误1130代码的方法
命令行登陆 mysql -uroot -p 输入密码后登陆 use mysql; select host,user from user ; grant allon *.*to root i ...