LeetCode刷题记

来源:互联网 发布:适合开发的linux系统 编辑:程序博客网 时间:2024/05/16 09:33
Maximum Depth of Binary Tree
/** * 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) {                int maxDepth = 0, depthLeft = 0, depthRight = 0;                if(root == null)            return 0;                    maxDepth = 1;                if(root.left != null){            depthLeft += maxDepth(root.left);        }                if(root.right != null){            depthRight += maxDepth(root.right);        }                maxDepth += ((depthLeft > depthRight) ? depthLeft : depthRight);        return maxDepth;            }}

0 0
原创粉丝点击