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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Allenlzcoder/article/details/79967415

#程序员面试金典——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);
    }
};
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页