本文主要是介绍面试150-69(Leetcode104二叉树的最大深度),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码:
0117
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public int maxDepth(TreeNode root) {if(root == null)return 0;else{int leftHeight = maxDepth(root.left);int rightHeight = maxDepth(root.right);return Math.max(leftHeight,rightHeight)+1;}}
}
这篇关于面试150-69(Leetcode104二叉树的最大深度)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!