Advertisement
gelita

maximum depth for a binary tree

Apr 3rd, 2020
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.38 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * public class TreeNode {
  4.  *     int val;
  5.  *     TreeNode left;
  6.  *     TreeNode right;
  7.  *     TreeNode(int x) { val = x; }
  8.  * }
  9.  */
  10. class Solution {
  11.     public int maxDepth(TreeNode root) {
  12.         if(root != null){
  13.             return Math.max(maxDepth(root.left),maxDepth(root.right)) + 1;
  14.         }
  15.         return 0;
  16.     }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement