zoukankan      html  css  js  c++  java
  • Maximum Depth of Binary Tree(二叉树最大深度)

    来源:https://leetcode.com/problems/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. 深度遍历,递归

    Java

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     public int maxDepth(TreeNode root) {
    12         if(root == null) {
    13             return 0;
    14         }
    15         int leftDepth = maxDepth(root.left) + 1;
    16         int rightDepth = maxDepth(root.right) + 1;
    17         return leftDepth > rightDepth ? leftDepth : rightDepth;
    18     }
    19 }

    Python

     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:
    15             return 0
    16         left_depth = self.maxDepth(root.left) + 1
    17         right_depth = self.maxDepth(root.right) + 1
    18         return left_depth if left_depth>right_depth else right_depth

    2. 广度遍历(逐层遍历),使用队列

    Java

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     public int maxDepth(TreeNode root) {
    12         Queue<TreeNode> queue = new LinkedList<TreeNode>();
    13         int depth = -1;
    14         queue.offer(root);
    15         TreeNode node = null;
    16         int queueSize = 0;
    17         while(!queue.isEmpty()) {
    18             queueSize = queue.size();
    19             while(queueSize-- > 0) {
    20                 node = queue.poll();
    21                 if(node == null) {
    22                     continue;
    23                 }
    24                 queue.offer(node.left);
    25                 queue.offer(node.right);
    26             }
    27             depth += 1;
    28         }
    29         return depth;
    30     }
    31 }

    Python

    # 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):
        def maxDepth(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            queue = [root]
            depth = -1
            while queue:
                depth += 1
                queue = [kid for node in queue if node for kid in (node.left, node.right)]
            return depth
  • 相关阅读:
    hdu 5902 Seam Carving
    hdu 5091 Beam Cannon
    hdu 1542 Atlantis
    hdu 2196 Computer
    第一个爬虫和测试
    排球比赛规则
    第十周博客作业
    科学计算可视化
    用matplotlib绘制图像
    面对对象学习
  • 原文地址:https://www.cnblogs.com/renzongxian/p/7530415.html
Copyright © 2011-2022 走看看