leetcode-剑指offer55-I-二叉树的深度
2020-09-02 10:14
591 查看
2020-9-2
这道题是经典的求二叉树深度问题,有递归和非递归两种解法。递归的思想就是二叉树的深度等于左右子树深度的最大值加一。非递归的话就类似于广度优先搜索,将每个节点的左右子树加入队列,当一层节点遍历结束后深度加一
https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/
[code]/** * 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; int max_depth = 0; max_depth = max(maxDepth(root->left),maxDepth(root->right)) + 1; return max_depth; } };
相关文章推荐
- Leetcode ||二叉树的最大深度
- LeetCode第111题:二叉树的最小深度
- Leetcode挑战:minimum-depth-of-binary-tree(二叉树最小深度)
- leetcode菜狗入门 | 104. 二叉树的最大深度
- LeetCode 104. 二叉树的最大深度
- *二叉树的深度(leetcode)
- LeetCode104--二叉树最大深度
- LeetCode----Maximum Depth of Binary Tree 求二叉树最大深度
- leetcode:minimum-depth-of-binary-tree(二叉树的最小深度)
- leetcode 100. Same Tree 二叉树DFS深度优先遍历
- LeetCode 二叉树的最大深度
- Leetcode 104 Maximum Depth of Binary Tree 二叉树的最大深度
- leetcode_111. Minimum Depth of Binary Tree 二叉树的最小深度
- Leetcode 111 python 二叉树的最小深度
- LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
- leetcode 236. Lowest Common Ancestor of a Binary Tree 最近公告祖先LCA + 二叉树 + 深度优先遍历DFS
- leetcode-1 Minimum Depth Of Binary Tree (二叉树叶子节点的最小深度)
- 【Golang】LeetCode-剑指Offer-面试题55 - I-二叉树的深度
- LeetCode 104 二叉树的最大深度 Java
- leetcode之二叉树类之二叉树深度系列-----104/111/110/108/109 二叉树最大/最小深度/AVL树的判断和由有序序列生成(牵扯分治相关,OJ105/106)