【问题标题】:Left View Of a Binary Tree二叉树的左视图
【发布时间】:2022-11-11 05:11:32
【问题描述】:

查找从二叉树左侧可见的所有节点的集合。

   vector<int> getLeftView(TreeNode<int> *root)
    {
         static vector<int> res;
       // Your code here
       if(root){
           res.push_back(root->data);
           if(root->left)
                getLeftView(root->left);
           else
                getLeftView(root->right);
       }
       return res;
}

对于一次单个测试用例,它可以正常工作。但是当运行多个测试用例时,向量中的先前值会附加新值。如何在运行下一个测试用例之前清除向量?

【问题讨论】:

  • 你没有;您重写代码以不使用静态对象。

标签: c++ data-structures binary-tree


【解决方案1】:

您使用了static,因为您需要在递归中使用向量的单个实例。但是static 不是办法;它导致整个程序中只有一个向量实例。

有多种解决方案,其中一种是将函数拆分为 API 和递归部分:

void getLeftViewRec(TreeNode<int> *root, vector<int> &res)
{
    if(root){
        res.push_back(root->data);
        if(root->left)
             getLeftView(root->left, res);
        else
             getLeftView(root->right, res);
    }
    return res;
}


vector<int> getLeftView(TreeNode<int> *root)
{
    vector<int> res;
    getLeftViewRec(root, res);
    return res;
}

现在发生的情况是,每次调用getLeftView 时,都会将一个新向量res 实例化为局部变量。然后它调用递归函数getLeftViewRec,该函数通过引用接收res,并通过递归调用将其传递给自身,因此递归使用单个向量,累积到其中。

【讨论】:

    【解决方案2】:

    使用队列和空指针来标记每个级别的第一个元素。我们在第一个插入一个空指针,当到达那个空指针时,我们将 bool 标记为 true,并将下一个元素作为我们的左视图元素,

    // C++ Code for the above iterative approach
    #include <bits/stdc++.h>
    using namespace std;
    
    // Tree Node Structure contains data, left and right
    // pointer
    struct Node {
        int data;
        struct Node *left, *right;
    };
    
    // A utility function to
    // create a new Binary Tree Node
    struct Node* newNode(int item)
    {
        struct Node* temp
            = (struct Node*)malloc(sizeof(struct Node));
        temp->data = item;
        temp->left = temp->right = NULL;
        return temp;
    }
    
    // function to get the left view of binary tree in vector
    vector<int> leftView(Node* root)
    {
    
        // store the left view
        vector<int> ans;
    
        // Base Case : Empty Tree
        if (!root)
            return ans;
        
    
        // Create an empty queue and enque root node and null
        queue<Node*> q;
        q.push(root);
        q.push(NULL);
        bool ok = true;
    
        // Traverse till queue is not empty
        while (!q.empty()) {
            // get the front node and dequeue it from queue
            auto it = q.front();
            q.pop();
    
            // if the front node is null do following steps
            if (it == NULL) {
                if (ok == false)
                    ok = true;
                
    
                if (q.size() == 0)
                    break;
                
                else
                    q.push(NULL);
                
            }
            // else do the following steps
            else {
                if (ok) {
                    ans.push_back(it->data);
                    ok = false;
                }
    
                if (it->left)
                    q.push(it->left);
                
                if (it->right)
                    q.push(it->right);
                
            }
        }
    
        // return the left view
        return ans;
    }
    
    // driver code to test above code on a test case
    int main()
    {
        /*
        Input :
                    10
                    / 
                    2    3
                /  / 
                7 8 12 15
                            /
                            14
    
        Output : 10 2 7 14
    */
        // let's build above shown tree
        Node* root = newNode(10);
        root->left = newNode(2);
        root->right = newNode(3);
        root->left->left = newNode(7);
        root->left->right = newNode(8);
        root->right->right = newNode(15);
        root->right->left = newNode(12);
        root->right->right->left = newNode(14);
    
        // call leftview function and store output in vec
        vector<int> vec = leftView(root);
        // traverse on left view and print each element
        for (int x : vec)
            cout << x << " ";
        cout << endl;
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      相关资源
      最近更新 更多