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 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution(object):
 9     def maxDepth(self, root):
10         """
11         :type root: TreeNode
12         :rtype: int
13         """
14         if not root: return 0
15
16         left = self.maxDepth(root.left)
17         right = self.maxDepth(root.right)
18         if left > right:
19             return 1 + left
20         else:
21             return 1 + right

    

时间: 2024-10-22 18:36:08

Maximum Depth of Binary Tree的相关文章

leetcode 104 Maximum Depth of Binary Tree二叉树求深度

Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question Solution 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

34: Maximum Depth of Binary Tree

/************************************************************************/        /*       34:     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 number of nodes along the longest path from the root node down to the farthest leaf node. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root

leetcode -day24 Maximum Depth of Binary Tree & Binary Tree Zigzag Level Order Traversal

1.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. class Solution { public: int maxDepth(TreeNode *root) { inM

LeetCode OJ - Minimum && Maximum Depth of Binary Tree

这两道题用递归的解法都很简单,只是稍有不同. 下面是AC代码: 1 /** 2 * Given a binary tree, find its minimum depth. 3 * the minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 4 * @param root 5 * @return 6 */ 7 public in

Maximum Depth of Binary Tree leetcode java

题目: 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     public int maxDepth(TreeNode root) {2         if(root==null)3         

LeetCode: 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. 题解: 题意比较清楚, 找到从root出发最长的一条路径的长度. 采用DFS即可. 相似的一道题: Minimux Depth of Binary Tree 解法: http://

Leetcode 树 Maximum Depth of Binary Tree

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Maximum Depth of Binary Tree Total Accepted: 16605 Total Submissions: 38287 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the ro

[LintCode] 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. Have you met this question in a real interview? Yes Example Given a binary tree as follow:

两道关于binary tree的水题 [Same Tree] [Maximum Depth of Binary Tree]

Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. Maximum Depth of Binary Tree Given a binary tree, find i