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

    Note: A leaf is a node with no children.

    Example:

    Given binary tree [3,9,20,null,null,15,7],

        3
       / 
      9  20
        /  
       15   7

    return its depth = 3.

     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         return helper(root);
    13     }
    14     
    15     private int helper(TreeNode root) {
    16         if (root == null) {
    17             return 0;
    18         }
    19         int left = helper(root.left);
    20         int right = helper(root.right);
    21         return Math.max(left, right) + 1;
    22     }
    23 }
  • 相关阅读:
    103
    101
    102
    100
    ByteView和Sink
    二叉排序树删除、搜索、插入的迭代实现
    怎样就地反转单链表?
    有序单链表的合并
    有序数组的合并
    静态表之整型数组的插入、删除、查找
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12000867.html
Copyright © 2011-2022 走看看