zoukankan      html  css  js  c++  java
  • 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.

    Recursive Version

     1 /**
     2  * Definition for binary tree
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public int maxDepth(TreeNode root) {
    12         // Start typing your Java solution below
    13         // DO NOT write main() function
    14         if(root == null){
    15             return 0;
    16         }
    17         int left = 0, right = 0;
    18         if(root.left != null){
    19             left = maxDepth(root.left);
    20         }
    21         
    22         if(root.right != null){
    23             right = maxDepth(root.right);    
    24         }
    25         
    26         return 1 + Math.max(left, right);
    27     }
    28 }
  • 相关阅读:
    教你作一份高水准的简历
    python并发
    阻塞,非阻塞,同步,异步
    python三层架构
    paramiko与ssh
    python-进程
    生产者消费者模型
    python-线程
    python-socket
    python-mysql
  • 原文地址:https://www.cnblogs.com/feiling/p/3258632.html
Copyright © 2011-2022 走看看