程序员面试金典——4.4输出单层结点

#程序员面试金典——4.4输出单层结点

Solution1:我的答案

关键是利用queue对二叉树进行层次遍历

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};*/

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class TreeLevel {
public:
    ListNode* getTreeLevel(TreeNode* root, int dep) {
        // write code here
        //利用栈进行层次遍历
        struct ListNode* result = NULL;
        if(root == NULL || dep <= 0)
            return result;
        queue<TreeNode*> seq_tree;
        vector<int> dupli;
        seq_tree.push(root);
        int cur = 1, next = 0, count_depth = 1;
        struct TreeNode* temp = NULL;
        while(!seq_tree.empty()) {
            if(count_depth == dep + 1)
                break;
            temp = seq_tree.front();
            if(count_depth == dep) {
                dupli.push_back(temp->val);
            }
            cur--;
            if(temp->left != NULL) {
                seq_tree.push(temp->left);
                next++;
            }
            if(temp->right != NULL) {
                seq_tree.push(temp->right);
                next++;
            }
            if(cur == 0) {
                cur = next;
                next = 0;
                count_depth++;
            }
            seq_tree.pop();
        }
        return BuildList(dupli);
    }
    struct ListNode* BuildList(const vector<int> &res) {
        struct ListNode* pHead = new ListNode(-1);
        struct ListNode* temp = pHead;
        for(int i = 0; i < res.size(); i++) {
            temp->next = new ListNode(res[i]);
            temp = temp->next;
        }
        return pHead->next;
    }
};

Solution2:递归遍历

参考网址:https://www.nowcoder.com/profile/6458593/codeBookDetail?submissionId=18190264
其实也可以用递归遍历实现,刚开始为深度为dep,每往下递归一层,则深度减一(dep=dep-1),当dep==1的时候,便输出那个元素,如果先递归左子树,那么则实现从左到右打印,如果先递归右子树,则实现从右往左打印。
好牛逼的思路,要学习一个!

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};*/

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class TreeLevel {
public:
    ListNode* getTreeLevel(TreeNode* root, int dep) {
        // write code here
        ListNode* list = new ListNode(-1);
        ListNode* listHead = list;
        get1(root,list,dep);
        return listHead->next;
    }
    void get1(TreeNode* root, ListNode* &list, int dep) {
        if (dep <= 0 || root == NULL)
            return;
        if (dep == 1) {
            ListNode* listnext = new ListNode(root->val);
            list->next = listnext;
            list = list->next;
            return ;
        }
        get1(root->left, list, dep - 1);
        get1(root->right, list, dep - 1);
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值