二叉树常见面试题

二叉树结构

struct BinaryTreeNode {
    int val;
    BinaryTreeNode *left;
    BinaryTreeNode *right;
};

1、二叉树的深度

int TreeDepth(BinaryTreeNode *root) {
    if(root == NULL)
        return 0;

    int leftDepth = TreeDepth(root->left);
    int rightDepth = TreeDepth(root->right);

    return (leftDepth > rightDepth) ? (leftDepth + 1) : (rightDepth + 1);
}

2、二叉树的宽度

int TreeWidth(BinaryTreeNode *root) {
    if(root == NULL)
        return 0;
    int maxWidth = 0;
    queue<BinaryTreeNode*> q;
    q.push(root);
    while(true) {
        int len = q.size();
        if(len == 0) break;
        while(len > 0) {
            BinaryTreeNode *node = q.front();
            q.pop();
            len--;
            if(node->left != NULL)
                q.push(node->left);
            if(node->right)
                q.push(node->right);
        }
        maxWidth = q.size() > maxWidth ? q.size() : maxWidth;
    }
    return maxWidth;
}

3、二叉树的镜像

void GetMirror(BinaryTreeNode *root) {
    if(root == NULL)
        return ;
    if(root->left == NULL && root->right == NULL)
        return ;

    BinaryTreeNode *tmp = root->left;
    root->left = root->right;
    root->right = tmp;

    if(root->left != NULL)
        GetMirror(root->left);
    if(root->right != NULL)
        GetMirror(root->right);
}

4、判断二叉树是否对称

bool IsSymmetrical(BinaryTreeNode *root) {
    return IsSymmetrical(root, root);
}

bool IsSymmetrical(BinaryTreeNode *root1, BinaryTreeNode *root2) {
    if(root1 == NULL && root2 == NULL)
        return true;
    if(root1 == NULL || root2 == NULL)
        return false;
    if(root1->val != root2->val)
        return false;
    return IsSymmetrical(root1->left, root2->right)
        && IsSymmetrical(root1->right, root2->left);
}

5、判断二叉树是否是平衡二叉树

bool IsBalanced(BinaryTreeNode *root, int *depth) {
    if(root == NULL) {
        *depth = 0;
        return true;
    }

    int left, right;
    if(IsBalanced(root->left, &left) && IsBalanced(root->right, &right)) {
        int diff = left - right;
        if(diff >= -1 && diff <= 1) {
            *depth = (left > right ? left : right) + 1;
            return true;
        }
    }
    return false;
}

bool IsBalanced(BinaryTreeNode *root) {
    int depth = 0;
    return IsBalanced(root, &depth);
}

6、不分行从上到下打印二叉树(所有的值一行输出)

void PrintFromTopToBottom(BinaryTreeNode *root) {
    if(root == NULL)
        return ;
    queue<BinaryTreeNode *> TreeNode;
    TreeNode.push(root);
    while(!TreeNode.empty()) {
        BinaryTreeNode *node = TreeNode.front();
        TreeNode.pop();
        printf("%d ", node->val);
        if(node->left != NULL)
            TreeNode.push(node->left);
        if(node->right != NULL)
            TreeNode.push(node->right);
    }
}

7、分行从上到下打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。

void PrintEachRowFromTopToBottom(BinaryTreeNode *root) {
    if(root == NULL)
        return ;
    queue<BinaryTreeNode *> TreeNode;
    TreeNode.push(root);
    int nextLevel = 0; // 下一层节点的数目
    int toBePrinted = 1; // 当前层中还没有打印的节点数
    while(!TreeNode.empty()) {
        BinaryTreeNode *node = TreeNode.front();
        printf("%d ", node->val);
        if(node->left != NULL) {
            TreeNode.push(node->left);
            ++nextLevel;
        }
        if(node->right != NULL) {
            TreeNode.push(node->right);
            ++nextLevel;
        }
        TreeNode.pop();
        --toBePrinted;
        if(toBePrinted == 0) {
            printf("\n");
            toBePrinted = nextLevel;
            nextLevel = 0;
        }
    }
}

8、之字形打印二叉树:第一层按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三层再按照从左到右的顺序打印,以此类推。

void PrintZigzagFromTopToBottom(BinaryTreeNode *root) {
    if(root == NULL)
        return ;
    stack<BinaryTreeNode*> levels[2];
    int current = 0;
    int next = 1;

    levels[0].push(root);
    while(!levels[0].empty() || !levels[1].empty()) {
        BinaryTreeNode *node = levels[current].top();
        levels[current].pop();

        printf("%d ", node->val);

        if(current == 0) {
            if(node->left != NULL)
                levels[next].push(node->left);
            if(node->right != NULL)
                levels[next].push(node->right);
        }
        else {
            if(node->right != NULL)
                levels[next].push(node->right);
            if(node->left != NULL)
                levels[next].push(node->left);
        }

        if(levels[current].empty()) {
            printf("\n");
            current = 1 - current;
            next = 1 - next;
        }
    }
}

9、二叉搜索树的第k大节点(中序遍历)

BinaryTreeNode *KthNode(BinaryTreeNode *root, int k) {
    if(root == NULL || k == 0)
        return NULL;
    return KthNodeCore(root, k);
}
BinaryTreeNode *KthNodeCore(BinaryTreeNode *root, int &k) {
    BinaryTreeNode *target = NULL;
    if(root->left != NULL)
        target = KthNodeCore(root->left, k);
    if(target == NULL) {
        if(k == 1)
            target = root;
        k--;
    }
    if(target == NULL && root->right != NULL)
        target = KthNodeCore(root->right, k);
    return target;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值