题目
解题思路
C#:
public class Solution {
public int MaxDepth(TreeNode root) {
if(root==null)
{
return 0;}
else
{
int leftDepth=MaxDepth(root.left);
int rightDepth=MaxDepth(root.right);
return Math.Max(leftDepth,rightDepth)+1;
}
}
}
python:
class Solution(object):
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
current_node_list = [root]
length = 0
while current_node_list:
tmp_node_list = []
for node in current_node_list:
if node.left is not None:
tmp_node_list.append(node.left)
if node.right is not None:
tmp_node_list.append(node.right)
current_node_list = tmp_node_list
length += 1
return length