korobushk

maxDepth r

May 16th, 2021
627
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Definition for a binary tree node.
  3.  * public class TreeNode {
  4.  *     int val;
  5.  *     TreeNode left;
  6.  *     TreeNode right;
  7.  *     TreeNode() {}
  8.  *     TreeNode(int val) { this.val = val; }
  9.  *     TreeNode(int val, TreeNode left, TreeNode right) {
  10.  *         this.val = val;
  11.  *         this.left = left;
  12.  *         this.right = right;
  13.  *     }
  14.  * }
  15.  */
  16. class Solution {
  17.         int depth = 0;
  18.     public int maxDepth(TreeNode root) {
  19.        
  20.            
  21.        if (root == null) {
  22.         return 0;                            
  23.     }
  24.     int left_depth = maxDepth(root.left);
  25.     int right_depth = maxDepth(root.right);
  26.     return Math.max(left_depth, right_depth) + 1;
  27. }
  28. }
  29.  
RAW Paste Data