Skip to main content
  1. Posts/

leetcode 104. Maximum Depth of Binary Tree(求一棵树的深度)

·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接

题意:求一棵树的深度。。。。

思路:。。。定义搞即可。。按照左右子树中大的算。。。因为据说是经典题(虽然并不觉得2333。。。所以记录下。。。

 1/**
 2 * Definition for a binary tree node.
 3 * struct TreeNode {
 4 *     int val;
 5 *     TreeNode *left;
 6 *     TreeNode *right;
 7 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8 * };
 9 */
10class Solution {
11public:
12
13        int dfs(TreeNode* root)
14        {
15            if (root==NULL) return 0;
16            return max(dfs(root->left),dfs(root->right))+1;
17        }
18
19    int maxDepth(TreeNode* root){
20        if (root==NULL) return 0;
21        int res = dfs(root);
22        return res;
23
24
25    }
26};

Related

codeforces #381 div2

·5 mins
http://codeforces.com/contest/740 A:现在有n个某种物品,要买k个使得n+k是4的倍数,可以的购买方案为a元1个,b元2个,c元3个,每种方案都可以买无限多。

sigh

·1 min
上编译原理课 写题…被老师干了QAQ 还好他的提问我没回答错Orz,不然更惨