LeetCode(104):二叉树的最大深度
2019-05-21 15:11
399 查看
通过的代码:
int maxDepth(struct TreeNode* root) { if(root==NULL) { return 0; } else { int l_deep=maxDepth(root->left); int r_deep=maxDepth(root->right); return (l_deep>r_deep?l_deep:r_deep)+1; } }
通过结果:
我发现,还是得处理好在底层的情况。如何从底层返回。
这个是我之前写的一个错误的版本:
int deep=0; if(root!=NULL) { int l_deep=maxDepth(root->left); int r_deep=maxDepth(root->right); deep=l_deep>r_deep?l_deep:r_deep; } else { return ++deep; } return deep;
我发现我就没怎么理解里面的过程。
自己调试一下,体会一下疯狂return时的场景。
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- LeetCode写题历程——104.二叉树的最大深度
- leetcode-腾讯精选练习(50 题)python #104 二叉树的最大深度
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | 深度优先与广度优先深入理解
- [LeetCode-104] Maximum Depth of Binary Tree(二叉树最大深度)
- [LeetCode] 104 二叉树最大深度
- LeetCode104--二叉树最大深度
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- LeetCode 104 二叉树的最大深度
- Leetcode 104 python 二叉树的最大深度
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- 【LeetCode】[104] 二叉树的最大深度——“递归”
- 二叉树的最大深度(leetcode-104)
- LeetCode 104 二叉树的最大深度
- leetcode之二叉树类之二叉树深度系列-----104/111/110/108/109 二叉树最大/最小深度/AVL树的判断和由有序序列生成(牵扯分治相关,OJ105/106)
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- LeetCode 104 Maximum Depth of Binary Tree(二叉树的最大深度)
- LeetCode 104--二叉树的最大深度
- Leetcode 104 Maximum Depth of Binary Tree 二叉树的最大深度
- leetcode 104求二叉树的最大深度