LeetCode OJ 513. Find Bottom Left Tree Value

LeetCode OJ 513. Find Bottom Left Tree Value


Description

Given a binary tree, find the leftmost value in the last row of the tree.

Example 1

Input:

2
/ \
1 3

Output:
1

Example 2

Input:

    1
   / \
  2   3
 /   / \
4   5   6
   /
  7

Output:
7

Note: You may assume the tree (i.e., the given root node) is not NULL.

方法一

BFS: 使用queue每次讀取一層數據,先遍歷左子樹,然後遍歷右子樹。每一層的第一個數存到BottomLeft中,這樣遍歷到最後一層得到的結果就是BottomLeft的正確結果。每層的第一個數是每一層queue的隊首元素front()。

代碼


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        if(!root->left && !root->right)
            return root->val;
        int BottomLeft, num;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()){
            BottomLeft = q.front()->val;
            num = q.size();
            while(num--){
                TreeNode* node = q.front();
                if(node->left)    q.push(node->left);
                if(node->right)    q.push(node->right);
                q.pop();
                //num--;
            }
        }
        return BottomLeft;
    }
};

方法二

BFS: 基於方法一的改進。每一層先遍歷右子樹,再遍歷左子樹。遍歷到的最後一個數就是最後一層的第一個數。

代碼


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        if(!root->left && !root->right)
            return root->val;

        //Solution 2
        int BottomLeft;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()){
            TreeNode* node = q.front();
            BottomLeft = node->val;
            q.pop();
            if(node->right)    q.push(node->right);
            if(node->left)    q.push(node->left);
        }

        return BottomLeft;
    }
};

方法三

DFS(Recursion): 深度優先搜索。每次先遞歸遍歷左子樹,這時深度depth會發生變化,與全局變量d對比,d比depth小的時候,說明進入了新的一層,此時節點的值就是這一層的最左邊的節點的值。所以遞歸深搜到最後一層的時候,返回的結果就是BottomLeft元素。

代碼

個人github代碼鏈接


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:
    int BottomLeft = 0, d = 0;
    void DFS(const TreeNode* root, int depth){
        if(!root)    return ;
        if(d < depth){
            d = depth;
            BottomLeft = root->val;
        }
        DFS(root->left, depth + 1);
        DFS(root->right, depth + 1);
    }
public:

    int findBottomLeftValue(TreeNode* root) {
        if(!root->left && !root->right)
            return root->val;

        //Solution 3: Recursion

        DFS(root, 1);
        return BottomLeft;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章