/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: The root of binary tree. * @return: An integer. */ public int maxDepth(TreeNode root) { // write your code here if(root==null) return 0; int leftDepth = maxDepth(root.left); int rightDepth = maxDepth(root.right); return leftDepth>rightDepth?(leftDepth+1):(rightDepth+1); } }
给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的距离。您在真实的面试中是否遇到过这个题?Yes哪家公司问你的这个题?AirbnbAlibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay Facebook Google Hulu Intel Linkedin Mi