Interview 39 - Depth of Binary Tree

topic link

To solve the depth of the binary tree, see the leetcode110 question for an extension.

Method 1: dfs.

1     private int TreeDepth(TreeNode root) {
2         if(root == null) {
3             return 0;
4         }
5         int l = TreeDepth(root.left);
6         int r = TreeDepth(root.right);
7         return (l > r) ? (l + 1) : (r + 1);
8     }
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325990950&siteId=291194637