LeetCode Hot100 | Day2 | 二叉树:二叉树的中序遍历二叉树的最大深度
LeetCode Hot100 | Day2 | 二叉树:二叉树的中序遍历&&二叉树的最大深度
注:和之前写过题解的部分且能够一遍写出来的不再写题解(可以写写注意点)
94.二叉树的中序遍历
94. 二叉树的中序遍历 - 力扣(LeetCode)
class Solution {
public:vector<int> res;void tra(TreeNode *t){if(t==nullptr)return;tra(t->left);res.push_back(t->val);tra(t->right);}vector<int> inorderTraversal(TreeNode* root) {tra(root);return res;}
};
104.二叉树的最大深度
104. 二叉树的最大深度 - 力扣(LeetCode)
完整代码:
class Solution {
public:int get_depth(TreeNode *t){if(t==nullptr)return 0;int l=get_depth(t->left);int r=get_depth(t->right);return 1+max(l,r);}int maxDepth(TreeNode* root) {if(root==nullptr)return 0;return get_depth(root);}
};