题解 | #牛群仰视图#

牛群仰视图

https://www.nowcoder.com/practice/0f37a18320c4466abf3a65819592e8be

题目考察的知识点:二叉树的遍历

题目解答方法的文字分析:仰视图,即就是这棵树的底部,也就是这棵树的叶子结点;中序遍历这棵树,遇到叶子结点保存。

本题解析所用的编程语言:c++

/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 *	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型vector
     */
    void inorder(TreeNode* root, vector<int>& v)
    {
        if (root == nullptr)
            return;
        inorder(root->left, v);
        if (root->left == nullptr && root->right == nullptr)
            v.push_back(root->val);
        inorder(root->right, v);
    }
    vector<int> bottomView(TreeNode* root) {
        // write code here
        vector<int> v;
        inorder(root, v);
        return v;
    }
};

全部评论

相关推荐

评论
1
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务