Maximum Depth of Binary Tree

来源:互联网 发布:windows下dmg转换cdr 编辑:程序博客网 时间:2024/06/06 03:10

/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public int maxDepth(TreeNode root) {        if(null == root){            return 0;                }        return 1 + Math.max(maxDepth(root.left),maxDepth(root.right));    }}
本题在leetcode中若使用return 1 + maxDepth(root.left)>maxDepth(root.right)?maxDepth(root.left):maxDepth(root.right); 会出现Timeout。可能是由于递归层次太深导致。


0 0
原创粉丝点击