Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int maxDepth(TreeNode root){
- if(root == null){
- return 0;
- }
- else if(root.left == null && root.right == null){
- return 0;
- }
- else{
- return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
- }
- }
- public int diameterOfBinaryTree(TreeNode root) {
- if(root == null){
- return 0;
- }
- else if(root.left == null && root.right == null){
- return 0;
- }
- int leftPath = root.left == null ? 0 : 1 + maxDepth(root.left);
- int rightPath = root.right == null ? 0 : 1 + maxDepth(root.right);
- return Math.max(Math.max(leftPath + rightPath, diameterOfBinaryTree(root.left)), diameterOfBinaryTree(root.right));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement