Algo. Leetcode. 104. Maximum Depth of Binary Tree

Decision

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Solution {
    public int MaxDepth(TreeNode root) {
        if (root == null)
          return 0;    
        var height = 1;
        if (root.left != null)
            
        {
          height = Math.Max(height, 1 + MaxDepth(root.left));    
        }    
        if (root.right != null)
        {
          height= Math.Max(height, 1 + MaxDepth(root.right));    
        }    
        
        return height;
    }
}
This entry was posted in Без рубрики. Bookmark the permalink.