leetcode 104 Maximum Depth of Binary Tree二叉树求深度
来源:互联网 发布:java web中启动线程池 编辑:程序博客网 时间:2024/05/29 14:53
Maximum Depth of Binary Tree
Total Accepted: 63668 Total Submissions: 141121 My Submissions
Question Solution
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) { if(NULL == root) return 0; int depth_l = maxDepth(root->left); int depth_r = maxDepth(root->right); return depth_l > depth_r ? depth_l + 1:depth_r + 1; }};
一行代码的解法:
int maxDepth(TreeNode *root){ return root == NULL ? 0 : max(maxDepth(root -> left), maxDepth(root -> right)) + 1;}
不用递归的解法:Breadth-first-search
int maxDepth(TreeNode *root){ if(root == NULL) return 0; int res = 0; queue<TreeNode *> q; q.push(root); while(!q.empty()) { ++ res; for(int i = 0, n = q.size(); i < n; ++ i) { TreeNode *p = q.front(); q.pop(); if(p -> left != NULL) q.push(p -> left); if(p -> right != NULL) q.push(p -> right); } } return res;}
不用递归的解法2
int maxDepth(TreeNode *root){ if (root == NULL) return 0; stack<TreeNode *> gray; stack<int> depth; int out = 0; gray.push(root); depth.push(1); while (!gray.empty()) { TreeNode *tmp = gray.top(); int num = depth.top(); gray.pop(); depth.pop(); if (tmp->left == NULL && tmp->right == NULL) { out = num > out ? num : out; } else { if (tmp->left != NULL) { gray.push(tmp->left); depth.push(num + 1); } if (tmp->right != NULL) { gray.push(tmp->right); depth.push(num + 1); } } } return out;}
python 的解决方案:
# Definition for a binary tree node# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # @param root, a tree node # @return an integer def maxDepth(self, root): def maxDepthHelper(root): if not root: return 0 return max(1+maxDepthHelper(root.left), 1+maxDepthHelper(root.right)) return maxDepthHelper(root)
1 0
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
- 【LeetCode】Maximum Depth of Binary Tree (二叉树最大深度)
- Leetcode Maximum Depth of Binary Tree 二叉树最大深度
- Maximum Depth of Binary Tree 求二叉树的高度/深度 @LeetCode
- [leetcode]Maximum Depth of Binary Tree (求二叉树的最大深度 C语言)
- leetcode系列(28)Maximum Depth of Binary Tree 求二叉树的最大深度
- LeetCode OJ-104. Maximum Depth of Binary Tree(求二叉树最大深度)
- 104/111 Maximum/Minimum Depth of Binary Tree(二叉树求深度)
- [LeetCode-104] Maximum Depth of Binary Tree(二叉树最大深度)
- LeetCode 104 Maximum Depth of Binary Tree(二叉树的最大深度)
- Leetcode No.104 Maximum Depth of Binary Tree 遍历二叉树的深度
- LeetCode-104 Maximum Depth of Binary Tree 二叉树的最大深度
- [leetcode]Maximum Depth of Binary Tree(二叉树最大深度、Depth-first search)
- Leetcode - Tree - 104. Maximum Depth of Binary Tree(DFS求二叉树最深深度)
- Maximum Depth of Binary Tree--二叉树的深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- 104.Maximum Depth of Binary Tree(二叉树深度)
- Win7 AERO主题变成灰色解决方法
- https://leetcode.com/problems/zigzag-conversion/
- Java基本概念与问题(三)
- java开发环境搭建
- [刷题]Minimum Path Sum
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
- Intent的详细介绍
- 编程之美---求数组的子数组之和的最大值
- HOOK钩子技术4 SSDT HOOK
- 322A - Ciel and Dancing 322B - Ciel and Flowers
- java swing基础和事件处理
- html5之新增标签学习
- 杭电ACM1000A + B Problem
- 三角蛇形填数