前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【数据结构与算法】一起搞定面试中的二叉树(一)

【数据结构与算法】一起搞定面试中的二叉树(一)

作者头像
用户1634449
发布2018-10-18 16:15:35
3770
发布2018-10-18 16:15:35
举报
文章被收录于专栏:Python专栏Python专栏

作者:IOExceptioner

最近总结了一些数据结构和算法相关的题目,这是二叉树相关面试题的总结,是用java实现的,由于篇幅有限,因此分为两部分,这是第一部分总结。先上二叉树的数据结构:

代码语言:javascript
复制
class TreeNode{
    int val;
    //左孩子
    TreeNode left;
    //右孩子
    TreeNode right;
}

二叉树的题目普遍可以用递归和迭代的方式来解

1. 求二叉树的最大深度

代码语言:javascript
复制
int maxDeath(TreeNode node){
    if(node==null){
        return 0;
    }
    int left = maxDeath(node.left);
    int right = maxDeath(node.right);
    return Math.max(left,right) + 1;
}

2. 求二叉树的最小深度

代码语言:javascript
复制
int getMinDepth(TreeNode root){
    if(root == null){
        return 0;
    }
    return getMin(root);
}

int getMin(TreeNode root){
    if(root == null){
        return Integer.MAX_VALUE;
    }
    if(root.left == null&&root.right == null){
        return 1;
    }
    return Math.min(getMin(root.left),getMin(root.right)) + 1;
}

3. 求二叉树中节点的个数

代码语言:javascript
复制
int numOfTreeNode(TreeNode root){
    if(root == null){
        return 0;
    }
    int left = numOfTreeNode(root.left);
    int right = numOfTreeNode(root.right);
    return left + right + 1;
}

4. 求二叉树中叶子节点的个数

代码语言:javascript
复制
int numsOfNoChildNode(TreeNode root){
    if(root == null){
        return 0;
    }
    if(root.left==null&&root.right==null){
        return 1;
    }
    return numsOfNodeTreeNode(root.left)+numsOfNodeTreeNode(root.right);
}

5. 求二叉树中第k层节点的个数

代码语言:javascript
复制
int numsOfkLevelTreeNode(TreeNode root,int k){
    if(root == null||k<1){
        return 0;
    }
    if(k==1){
        return 1;
    }
    int numsLeft = numsOfkLevelTreeNode(root.left,k-1);
    int numsRight = numsOfkLevelTreeNode(root.right,k-1);
    return numsLeft + numsRight;
}

6. 判断二叉树是否是平衡二叉树

代码语言:javascript
复制
boolean isBalanced(TreeNode node){
    return maxDeath2(node)!=-1;
}

int maxDeath2(TreeNode node){
    if(node == null){
        return 0;
    }
    int left = maxDeath2(node.left);
    int right = maxDeath2(node.right);
    if(left==-1||right==-1||Math.abs(left-right)>1){
        return -1;
    }
    return Math.max(left, right) + 1;
}

7.判断二叉树是否是完全二叉树

代码语言:javascript
复制
boolean isCompleteTreeNode(TreeNode root){
    if(root == null){
        return false;
    }
    Queue<TreeNode> queue = new LinkedList<TreeNode>();
    queue.add(root);
    boolean result = true;
    boolean hasNoChild = false;
    while(!queue.isEmpty()){
        TreeNode current = queue.remove();
        if(hasNoChild){
            if(current.left!=null||current.right!=null){
                result = false;
                break;
            }
        }else{
            if(current.left!=null&&current.right!=null){
                queue.add(current.left);
                queue.add(current.right);
            }else if(current.left!=null&&current.right==null){
                queue.add(current.left);
                hasNoChild = true;
            }else if(current.left==null&&current.right!=null){
                result = false;
                break;
            }else{
                hasNoChild = true;
            }
        }
    }
    return result;
}

8. 两个二叉树是否完全相同

代码语言:javascript
复制
boolean isSameTreeNode(TreeNode t1,TreeNode t2){
    if(t1==null&&t2==null){
        return true;
    }
    else if(t1==null||t2==null){
        return false;
    }
    if(t1.val != t2.val){
        return false;
    }
    boolean left = isSameTreeNode(t1.left,t2.left);
    boolean right = isSameTreeNode(t1.right,t2.right);
    return left&&right;
}

9. 两个二叉树是否互为镜像

代码语言:javascript
复制
boolean isMirror(TreeNode t1,TreeNode t2){
    if(t1==null&&t2==null){
        return true;
    }
    if(t1==null||t2==null){
        return false;
    }
    if(t1.val != t2.val){
        return false;
    }
    return isMirror(t1.left,t2.right)&&isMirror(t1.right,t2.left);
}

10. 翻转二叉树or镜像二叉树

代码语言:javascript
复制
TreeNode mirrorTreeNode(TreeNode root){
    if(root == null){
        return null;
    }
    TreeNode left = mirrorTreeNode(root.left);
    TreeNode right = mirrorTreeNode(root.right);
    root.left = right;
    root.right = left;
    return root;
}

11. 求两个二叉树的最低公共祖先节点

代码语言:javascript
复制
TreeNode getLastCommonParent(TreeNode root,TreeNode t1,TreeNode t2){
    if(findNode(root.left,t1)){
        if(findNode(root.right,t2)){
        return root;
        }else{
            return getLastCommonParent(root.left,t1,t2);
        }
    }else{
        if(findNode(root.left,t2)){
            return root;
        }else{
        return getLastCommonParent(root.right,t1,t2)
        }
    }
}
    // 查找节点node是否在当前 二叉树中
boolean findNode(TreeNode root,TreeNode node){
    if(root == null || node == null){
        return false;
    }
    if(root == node){
        return true;
    }
    boolean found = findNode(root.left,node);
    if(!found){
        found = findNode(root.right,node);
    }
    return found;
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-10-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python专栏 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档