leetcode -- Maximum Depth of Binary Tree

Maximum Depth of Binary Treenode

Given a binary tree, find its maximum depth.code

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.ip

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
         if(root==null){
            return 0;
        }
        int ldep = maxDepth(root.left);
        int rdep = maxDepth(root.right);
        return ldep> rdep? ldep+1: rdep +1;
    }
}
相關文章
相關標籤/搜索