LeetCode 面试题55 - I. 二叉树的深度

题目链接:https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/

输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。

例如:

给定二叉树 [3,9,20,null,null,15,7],

3
/ \
9 20
/ \
15 7
返回它的最大深度 3 。

 

提示:

节点总数 <= 10000

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     struct TreeNode *left;
 6  *     struct TreeNode *right;
 7  * };
 8  */
 9 
10 int maxDepth(struct TreeNode* root){
11     if(root==NULL) return 0;
12     int x=maxDepth(root->left)+1;
13     int y=maxDepth(root->right)+1;
14     return x>y?x:y;
15 }

 

posted @ 2020-03-03 23:05  wydxry  阅读(136)  评论(0编辑  收藏  举报
Live2D