LeetCode 104. Maximum Depth of Binary Tree

来源:互联网 发布:担保行业 知乎 编辑:程序博客网 时间:2024/05/17 20:45
/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public int maxDepth(TreeNode root) {        int leftDepth = 1;    int rightDepth = 1;    if (root == null) {    return 0;    }    if (root.left != null) {    leftDepth += maxDepth(root.left);    }    if (root.right != null) {    rightDepth += maxDepth(root.right);    }    return leftDepth > rightDepth ? leftDepth : rightDepth;    }}

0 0
原创粉丝点击