4.7

Topic 4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree.

方法1With Links to Parents: trace p and q’s paths up until they intersect. This would require a) being able to mark nodes as isVisited or b) being able to store some data in an additional data structure, such as a hash table.

方法2Follow a chain if p and q are on the same side. Else, found the first common ancestor.

Time is O(n) on a balanced tree. This is because covers is called on 2n nodes in the first call, after that, branches left or right, at which point covers will be called on n, then n/2. This results in a runtime of O(n)??】. We cannot do better because we need to potentially look at every node in the tree. But we may be able to improve it by a constant multiple.

方法3Each sbutree is searched over and over again. We should only need to search the entire tree once to find p and q, then bubble up the findings to earlier nodes in the stack.

----Returns p, if root’s subtree includes p (and not q)

----Returns q, if root’s subtree includes q (and no p)

----Returns null, if neither p nor q are in root’s subtree.

----Else, returns the common ancestor of q and p.

----Final case: commonAncestor(n.left) & commonAncestor(n.right) both return non-null value.     

方法4First search through the entrie tree to make sure both nodes exist, then use a method just return TreeNode. 【这里方法3和方法4需要时间再仔细看一下,问到了再看】

public class c4_7 {
	// Checks how many “special” nodes are located under this root
		public static int covers(TreeNode root, TreeNode p, TreeNode q) {
			int num=0;
			if (root == null) return num;
			if (root == p || root == q) num+= 1;
			num+=covers(root.left,p,q);
			if(num==2) // found p and q,这部分要不要问题都不大,可以减少点时间
				return num;
			return num+covers(root.right,p,q);
		}
	
		public static TreeNode commonAncestor1(TreeNode root, TreeNode p, TreeNode q) {
			if (q == p) return p;
			if (root==q||root==p) return root;//这一句必须有,因为底下的covers没有考虑头结点,有可能一个节点在左边,一个为头,nodesFromLeft=1
			int nodesFromLeft = covers(root.left, p, q); // Check left side
			if (nodesFromLeft == 2) {//如果两个都在左边,返回root.left,依次递归
				if(root.left == p || root.left == q) return root.left;
				else return commonAncestor1(root.left, p, q);
			} 
				
			int nodesFromRight = covers(root.right, p, q);// Check right side
			if(nodesFromRight == 2) {
				if(root.right == p || root.right == q) return root.right;
				else return commonAncestor1(root.right, p, q);
			}  
			
			else return root;//剩下的只有一种情况,就是一边一个,所以判断条件可以不写
		}
		       
		public static class Result {
			public TreeNode node;
			public boolean isAncestor;
			public Result(TreeNode n, boolean isAncestor) {
				node = n;
				this.isAncestor = isAncestor;
			}
		}
		
		public static Result commonAncestorHelper(TreeNode root, TreeNode p, TreeNode q) {
			if (root == null) {
				return new Result(null, false);
			}
			if (root == p && root == q) {// q==p的情况
				return new Result(root, true);
			}

			Result rx = commonAncestorHelper(root.left, p, q);
			if (rx.isAncestor) { 
				return rx;
			}

			Result ry = commonAncestorHelper(root.right, p, q);
			if (ry.isAncestor) { // Found common ancestor
				return ry;
			}

			if (rx.node != null && ry.node != null) {//1.左右都有返回值,说明左右都找到了,就是它了
				return new Result(root, true); 
			} else if (root == p || root == q) {//就在这一点上
				boolean isAncestor = rx.node != null || ry.node != null ? true : false;
				return new Result(root, isAncestor);//在这一点上,如果左边或者右边有返回值,就是它了;没有就不是它
			} else {
				return new Result(rx.node != null ? rx.node : ry.node, false);//2.左边或右边有返回值,就一个,返回这个返回值; 3. ..
		   //三种情况:1. 左右都有返回值,就是它;2. 左边或右边有返回值,就一个,返回这个返回值; 3. 左右都没返回值,返回Null
			}
		}	

		public static TreeNode commonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
			Result r = commonAncestorHelper(root, p, q);
			if (r.isAncestor) {
				return r.node;
			}
			return null;
		}	

	public static void main(String[] args) {
		int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
		TreeNode root = TreeNode.createMinimalBST(array);
		TreeNode p = root.find(6);
		TreeNode q = root.find(10);
		TreeNode ancestor = commonAncestor2(root, p, q);
		System.out.println(ancestor.data);
	}
}
//结果
8



 

python023基于Python旅游景点推荐系统带vue前后端分离毕业源码案例设计 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值