LeetCode 104:Maximum Depth of Binary Tree
来源:互联网 发布:qq赚钱软件 编辑:程序博客网 时间:2024/05/16 23:58
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.
给定一颗二叉树,找到它的最大深度。
最大深度定义为从根节点到叶节点的最长路径。
思路其实挺简单。。。分别记录左右枝的最大值,返回较大的那一个即可
/** * 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) { int max_left=1,max_right=1; if(root==NULL) return 0; else if(root->left==NULL&&root->right==NULL) return 1; if(root->left!=NULL) max_left=(1+maxDepth(root->left)); if(root->right!=NULL) max_right=(1+maxDepth(root->right)); return max_left>max_right?max_left:max_right; }};
0 0
- Leetcode 104 Maximum Depth of Binary Tree
- LeetCode(104)Maximum Depth of Binary Tree
- LeetCode 104 Maximum Depth of Binary Tree
- LeetCode: Maximum Depth of Binary Tree [104]
- Leetcode(104)Maximum Depth of Binary Tree
- [leetcode 104] Maximum Depth of Binary Tree
- LeetCode 104 Maximum Depth of Binary Tree
- leetCode #104 Maximum Depth of Binary Tree
- LeetCode 104: Maximum Depth of Binary Tree
- LeetCode 104 Maximum Depth of Binary Tree
- leetcode || 104、Maximum Depth of Binary Tree
- LeetCode 104: Maximum Depth of Binary Tree
- leetcode[104]:Maximum Depth of Binary Tree
- Leetcode[104]-Maximum Depth of Binary Tree
- Leetcode---(104)Maximum Depth of Binary Tree
- leetcode-104-Maximum Depth of Binary Tree
- Leetcode #104 Maximum Depth of Binary Tree
- leetcode 104: Maximum Depth of Binary Tree
- android 图片缓存
- [Python标准库]re——正则表达式[一]
- Apriori关联规则(购物篮模型)
- AngularJS
- android 内存优化方式整理
- LeetCode 104:Maximum Depth of Binary Tree
- PredicateAP
- 时序预测技术
- VC计算器实现
- python学习---字典
- Swift:可选类型(optional)与构造函数
- OS X EI Capitan安装后需要的配置
- 消息循环NSRunLoop
- React Native真机调试