「數據結構與算法」一起搞定面試中的二叉樹

「數據結構與算法」一起搞定面試中的二叉樹

最近總結了一些數據結構和算法相關的題目,這是二叉樹相關面試題的總結,是用java實現的,由於篇幅有限,因此分為兩部分,這是第一部分總結。先上二叉樹的數據結構:

class TreeNode{
int val;
//左孩子
TreeNode left;
//右孩子
TreeNode right;
}

二叉樹的題目普遍可以用遞歸迭代的方式來解

1. 求二叉樹的最大深度

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. 求二叉樹的最小深度

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. 求二叉樹中節點的個數

int numOfTreeNode(TreeNode root){
if(root == null){
return 0;
}
int left = numOfTreeNode(root.left);
int right = numOfTreeNode(root.right);
return left + right + 1;
}

4. 求二叉樹中葉子節點的個數

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層節點的個數

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. 判斷二叉樹是否是平衡二叉樹

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.判斷二叉樹是否是完全二叉樹

什麼是完全二叉樹呢?參見通俗易懂講解 二叉搜索樹(請戳我)

boolean isCompleteTreeNode(TreeNode root){
if(root == null){
return false;
}

Queue queue = new LinkedList();
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. 兩個二叉樹是否完全相同

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. 兩個二叉樹是否互為鏡像

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鏡像二叉樹

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. 求兩個二叉樹的最低公共祖先節點

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;
}


分享到:


相關文章: