二叉树遍历,查找,删除,翻转(百度面试)

题目描述(Easy)

1. 二叉树的前序、中序遍历,非递归实现:Morris算法:https://blog.csdn.net/ansizhong9191/article/details/82624797

2. 二叉树的查找:https://leetcode-cn.com/problems/search-in-a-binary-search-tree/

3. 二叉树的删除:

4. 二叉树的翻转:https://leetcode-cn.com/problems/invert-binary-tree/

前序遍历Morris实现:

/**
 * 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:
    vector preorderTraversal(TreeNode* root) {
        // Morris algorithm
        TreeNode *cur = root, *prev = root;
        vector ans;
        
        while (cur) {
            if (cur->left == nullptr) {
                ans.push_back(cur->val);
                cur = cur->right;
            } else {
                prev = cur->left;
                while (prev->right != nullptr && prev->right != cur)
                    prev = prev->right;
                
                if (prev->right == nullptr) {
                    ans.push_back(cur->val);
                    prev->right = cur;
                    cur = cur->left;
                } else {
                    prev->right = nullptr;
                    cur = cur->right;
                }
            }
        }
        
        return ans;
        
    }
};

二叉树的查找:

/**
 * 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:
    TreeNode* searchBST(TreeNode* root, int val) {
        if (!root || root->val == val) return root;
        
        TreeNode* left = searchBST(root->left, val);
        TreeNode* right = searchBST(root->right, val);
        
        if (left == nullptr && right == nullptr) return nullptr;
        else return left != nullptr ? left : right;
    }
};

二叉搜索树的查找:

/**
 * 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:
    TreeNode* searchBST(TreeNode* root, int val) {
        if (!root) return root;
        
        TreeNode* target = root;
        while (target) {
            if (val > target->val) {
                target = target->right;
            } else if (val < target->val) {
                target = target->left;
            } else {
                return target;
            }
        }
        
        return nullptr;
    }
};

二叉树的删除:

二叉树的翻转:

/**
 * 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:
    TreeNode* invertTree(TreeNode* root) {
        if (!root) return root;
        
        root->left = invertTree(root->left);
        root->right = invertTree(root->right);
        
        TreeNode* temp = root->left;
        root->left = root->right;
        root->right = temp;
        
        return root;
    }
};

 

你可能感兴趣的:(OJ真题)