Leetcode 114二叉树的最大深度

题目

在这里插入图片描述

解题思路

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

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Pang_ling/article/details/106003084