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 #…
Maximum Depth of Binary Tree 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. 求二叉树的高度,即从根节点到叶节点上所有节点的数量. 解题思路: 这题是难度系数1,频率1的题目……用到的知识点是二叉树D…
Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum depth, we BFS from the root and record the depth. For each level we add 1 to the depth and return the depth value when we reach a leaf. The python code is…
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution(object):    def maxDepth(self, root):        if…
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 和上一题对应,求二叉树的最小深度. 解题思路: 参考上一题Maximun Depth of Binary Tree中最后那…
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. 很简单,直接上代码: /** * Definition for a binary tree node. * public class TreeNode { * int val; *…
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. 求二叉树深度,直接看代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tr…
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. 递归解题思路: ①结点为空时,返回0:②当前结点的深度 = max(两个孩子结点各自最大深度)+ 1 /** * Definition for binary tree * stru…
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. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: /** * Definition for binary tree * public class Tre…
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最大深度 3 . DFS 递归算法,简单的二叉树题.如果看不懂代码,建议学习一下二叉树,这可是基础啊! 给出代码: 递归法: /** * Definition for a binary tree…