【数据结构】二叉树的堂兄弟 Cousins in Binary Tree

二叉树的堂兄弟 Cousins in Binary Tree

在二叉树中,根节点位于深度 0 处,每个深度为 k 的节点的子节点位于深度 k+1 处。

如果二叉树的两个节点深度相同,但 父节点不同 ,则它们是一对堂兄弟节点。

我们给出了具有唯一值的二叉树的根节点 root ,以及树中两个不同节点的值 x 和 y 。

只有与值 x 和 y 对应的节点是堂兄弟节点时,才返回 true 。否则,返回 false。

思路

按照题目的要求,需要计算2个节点的深度,如果深度一样,就比较父节点是否是同一个节点。

原始的做法:

设定一个方法计算节点的深度。

设定一个方法计算节点的父节点。

public boolean isCousins(TreeNode root, int x, int y) {
	if(root==null){
		return false;
	}
	int xdepth = getdepth(root, x,0);
	int ydepth = getdepth(root, y,0);
     
	if(xdepth!=ydepth){
		return false;
	}
	TreeNode p = findxparent(root,x);
	TreeNode q = findxparent(root,y);
	return !(p.val==q.val);
}

public TreeNode findxparent(TreeNode root, int x){
	if(root==null){
		return null;
	}

    TreeNode t=null;
	System.out.println("root 1 "+root.val);
	if(root.left!=null){
		if(root.left.val==x){
			return root;
		}
		else{
			t = findxparent(root.left,x);
		}
	}
	if(t!=null){
        return t;
    }
	
	if(root.right!=null){
		if(root.right.val==x){
			return root;
		}
		else{
			t = findxparent(root.right,x);
		}
	}
	return t;
}

public int getdepth(TreeNode root, int x,int depth){
	if(root==null){
		return -1;
	}
	if(root.val==x){
		return depth;
	}
	depth +=1;
	int left = getdepth(root.left,x,depth);
	if(left>0){
		return left;
	}
	
	int right = getdepth(root.right,x,depth);
	if(right>0){
		return right;
	}
	return 0;
}

DFS在求节点的深度的同时,保存节点的父节点信息

public boolean isCousins(TreeNode root, int x, int y) {
	if(root==null){
		return false;
	}
    TreeNode[] xparent =new TreeNode[1];
	TreeNode[] yparent =new TreeNode[1];
	int xdepth = getdepth(root, x,xparent);
	int ydepth = getdepth(root, y,yparent);
	return xdepth==ydepth&& xparent[0]!=yparent[0] ;
}
 

public int getdepth(TreeNode root, int x,TreeNode[] parent){
	if(root==null){
		return -1;
	}
	if(root.val==x){
		return 0;
	}
	parent[0] =root;
	int res=0;
	
	res = getdepth(root.left,x,parent);
	if(res!=-1){
		return res+1;
	}
	parent[0] =root;
	res = getdepth(root.right,x,parent);
	if(res!=-1){
		return res+1;
	}
	return -1;
}

使用BFS遍历的过程中保存节点的深度和父节点信息。需要额外的数据结构,来保存信息。

public class Data{
        int depth;
        TreeNode father;
        TreeNode node;
        Data(TreeNode n,TreeNode f,int d){
            depth =d;
            father =f ;
            node =n;
        }
    }

public boolean isCousins(TreeNode root, int x, int y) {
	if(root==null){
		return false;
	}
	int dx =-1;
	int dy =-1;
	TreeNode xfather =null;
	TreeNode yfather =null;
	Queue<Data> queue = new LinkedList<Data>();
	Data data = new Data(root,null,0);
	queue.offer(data);
	
	while(!queue.isEmpty()){
		Data t = queue.peek();
		TreeNode cur = t.node;
		if(cur.val==x){
			dx = t.depth;
			xfather = t.father;
		}
		if(cur.val==y){
			dy = t.depth;
			yfather = t.father;
		}
		
		if(cur.left!=null){
			queue.offer(new Data(cur.left,cur,t.depth+1));
		}
		if(cur.right!=null){
			queue.offer(new Data(cur.right,cur,t.depth+1));
		}
		queue.poll(); 
	}
	return dx==dy && xfather!=yfather ;	 
}

Tag

DFSBFS

原文地址:https://www.cnblogs.com/dreamtaker/p/15368032.html