for Robot Artificial Inteligence

31. Convert Sorted Array to Binary Search Tree(我的缺点是TREE,以后有代码考试需要集中tree学习)

|

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        return sortedArrayToBST(nums, 0, nums.size());
    }
    TreeNode* sortedArrayToBST(vector<int>& nums, int start, int end)
    {
        if(end<=start)
        {
            return NULL;
        }
        int mid = (start+end)/2;
        TreeNode* root = new TreeNode(nums[mid]);
        root->left = sortedArrayToBST(nums,start, mid);
        root->right = sortedArrayToBST(nums,mid+1, end);
        return root;
    }
};

Comment  Read more

30.Binary Tree Level Order Traversal(Difficult)

|

Pre-inorder Traversal method

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
vector<vector<int>> level;
class Solution {
public:
    void building_level(TreeNode* root, int n)
    {
        if(root==NULL) return;
        if(level.size() == n)
        {
            level.push_back(vector<int>()); // create array in vector
        }
        level[n].push_back(root->val);
        building_level(root->left, n+1);
        building_level(root->right, n+1);
    }
    vector<vector<int>> levelOrder(TreeNode* root) {
        building_level(root, 0);
        return level;
    }
};

Iteration Method

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        if(!root)
            return {};
        vector<int> row;
        vector<vector<int>> result;
        queue<TreeNode*> q;
        q.push(root);
        int count = 1;
        while(!q.empty())
        {
            if(q.front()->left)
                q.push(q.front()->left);
            if(q.front()->right)
                q.push(q.front()->right);
            row.push_back(q.front()->val);
            q.pop();
            if(--count==0)
            {
                result.push_back(row);
                row.clear();
                count = q.size();
            }
        }
        return result;

    }
};
  • Tree문제를 풀떄는 가시적으로 보자. [숲을 보지말고 나무를 보자]

Comment  Read more

29. Symmetric Tree

|

Recursive Solution

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

    }
    bool isSymmetric(TreeNode* l, TreeNode* r)
    {
        if(!l && !r)
            return true;
        else if(!l || !r)
        {
            return false;
        }
        if(l->val != r->val)
        {
            return false;
        }
        return isSymmetric(l->left,r->right) && isSymmetric(l->right,r->left); // 동시에 확인
    }
};

Comment  Read more

28. Validate Binary Search Tree[어려움]

|

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        return isValidBST(root, NULL, NULL);
    }
    bool isValidBST(TreeNode* root, TreeNode* minNode, TreeNode* maxNode)
    {
        if(!root)
            return true;
        if(minNode && root->val <= minNode->val || maxNode && root->val >= maxNode->val)
        {
            return false;
        }
        return isValidBST(root->left,minNode,root) && isValidBST(root->right, root, maxNode); // 동시에 확인
    }
};

Comment  Read more

27. Maximum Depth of Binary Tree

|

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        return (root==NULL)?0:max(maxDepth(root->left),maxDepth(root->right))+1; // 동시에 확인

    }
};
  • Tree 문제는 대부분 Recursive로 해결한다.
  • iteration로 풀떄는 queue를 이용한다.

Comment  Read more