LeetCode,LeetCode——Maximum Depth of Binary Tree

 2023-11-19 阅读 28 评论 0

摘要:LeetCode——Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Answer /*** Definition for a binary tree node

LeetCode——Maximum Depth of Binary Tree

Question

Given a binary tree, find its maximum depth.

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

Answer

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/class Solution {
public:// 求樹的高度int maxDepth(TreeNode* root) {if (!root)return 0;else {int i = maxDepth(root->left);int j = maxDepth(root->right);return max(i, j) + 1;}}
};

轉載于:https://www.cnblogs.com/zhonghuasong/p/6658654.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/180102.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息