TreeviewCopyright © aleen42 all right reserved, powered by aleen42
104. 二叉树的最大深度
https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
Java
/*
* @Author: Goog Tech
* @Date: 2020-07-21 21:48:51
* @Description: https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
* @FilePath: \leetcode-googtech\#104. Maximum Depth of Binary Tree\Solution.java
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
// 后序遍历(DFS): 递归法
public int maxDepth(TreeNode root) {
return root == null ? 0 : Math.max(maxDepth(root.left),maxDepth(root.right)) + 1;
}
}
Python
'''
@Author: Goog Tech
@Date: 2020-07-21 21:48:59
@Description: https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
@FilePath: \leetcode-googtech\#104. Maximum Depth of Binary Tree\Solution.py
'''
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
# 后序遍历(DFS): 递归法
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
# lDepth = self.maxDepth(root.left)+1
# rDepth = self.maxDepth(root.right)+1
# return 0 if not root else lDepth if lDepth > rDepth else rDepth
return 0 if not root else max(self.maxDepth(root.right),self.maxDepth(root.left)) + 1