maximum-depth-of-binary-tree binary maximum depth

Subject description:

Seeking to set the maximum depth of a binary tree,

The maximum depth is the number of nodes of the root of the tree to the leaf node farthest longest path.

Problem-solving ideas:

Recursive, recursive binary tree is commonly used method. 

/**
 * 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) {
        if(root == null)
            return 0;
        int maxnum = 1;
        maxnum = Math.max(maxnum + maxDepth(root.left), maxnum + maxDepth(root.right));
        return maxnum;
    }
}

 

Published 52 original articles · won praise 6 · views 8991

Guess you like

Origin blog.csdn.net/PMPWDF/article/details/104031255