Cracking coding interview (4.5)二叉树按中序遍历次序返回某节点下一个节点

4.5 Write an algorithm to find the ‘next’ node (e.g., in-order successor) of a given 

node in a binary search tree where each node has a link to its parent.

1.方法一:直接对二叉树进行中序遍历,获得中序遍历的节点列表,然后返回该列表中目标节点的下一个节点即可,该方法无需parent

2.方法二:根据目标节点target分情况:

1).若target.right != null,然会以target.right为树根的树的最左子孙节点

ELSE target.right == null

2).

2.1)若target == target.parent.left(target是他父亲的左孩子,则target的下一个节点就是

它的parent,值得注意的是若target的父节点为null,则target一定为root节点,它的next节点此时一定

为null).

2.2)若target == target.parent.right,回溯target,直到target的一个祖先X,且祖先X是他

父亲的左孩子,返回最先X的parent节点,反之target一直回溯一定会到root节点,此时返回null。


import java.util.ArrayList;
import java.util.Iterator;

class TreeNode{
	int val;
	TreeNode left;
	TreeNode right;
	TreeNode parent;
	public TreeNode(int val){
		this.val = val;
		this.left = null;
		this.right = null;
		this.parent = null;
	}
}

public class Solution{
	//In-Order, gain the result list of In-Order
	//then find the next node of target by traverse the result list
	public static TreeNode findNextNode(TreeNode root, TreeNode target){
		if(root == null)
			return null;
		ArrayList<TreeNode>	list =  new ArrayList<TreeNode>();	
		recurInOrder(root.left, list);
		list.add(root);
		recurInOrder(root.right, list);
		//find next node
		Iterator it = list.iterator();
//		System.out.println("list.size()="+list.size());//
		int value = -1;
		while(it.hasNext()){
			value = ((TreeNode)it.next()).val;
			System.out.print(value+" ");///
			if(value == target.val){
				System.out.println();///
				if(it.hasNext())
					return (TreeNode)it.next();
				else
					return null;
			}
		}
		System.out.println();///
		return null;
	}
	private static void recurInOrder(TreeNode tNode, ArrayList<TreeNode> list){
		if(tNode != null){
			recurInOrder(tNode.left, list);
			list.add(tNode);
			recurInOrder(tNode.right, list);
		}		
	}
	public static TreeNode getNextNodeInOrder(TreeNode target){
		if(target == null)	
			return null;
		TreeNode tNode = null;
		if(target.right != null)
			for(tNode = target.right;tNode.left != null;tNode = tNode.left)
				;
		else{	
			//target is left child
/*			if(target.parent == null || target == target.parent.left){
				//actually when target.parent == null, target is root node
				//only root node hasn't parent 
				tNode = target.parent;
			// target is right child
			}else{ */
			//up util find a node which is left child of a parent
				for(tNode = target;;)
					if(tNode.parent == null || tNode == tNode.parent.left){
						tNode = tNode.parent;
						break;
					}else
						tNode = tNode.parent;
			}
			
		return tNode;
	}
	
	public static void main(String[] args){
		TreeNode root = new TreeNode(0);
		root.left = new TreeNode(1);
		root.left.left = new TreeNode(3);	
		root.left.left.left = new TreeNode(7);	
		root.left.right = new TreeNode(4);
		root.right = new TreeNode(2);
		root.right.left = new TreeNode(5);
		root.right.left.left = new TreeNode(8);
		root.right.right = new TreeNode(6);
		
		TreeNode tNode = Solution.findNextNode(root, new TreeNode(0));
		System.out.println("R<0>:"+(tNode != null ? tNode.val : null));
		tNode = Solution.findNextNode(root, new TreeNode(6));
		System.out.println("R<6>:"+(tNode != null ? tNode.val : null));
		tNode = Solution.findNextNode(root, new TreeNode(8));
		System.out.println("R<8>:"+(tNode != null ? tNode.val : null));
		
		root.parent = null;
		root.left.parent = root.right.parent = root;
		root.left.left.parent = root.left.right.parent = root.left;
		root.left.left.left.parent = root.left.left;
		root.right.left.parent =  root.right.right.parent = root.right;
		root.right.left.left.parent =  root.right.left;
		
		tNode = Solution.getNextNodeInOrder(root);
		System.out.println("RR<0>:"+(tNode != null ? tNode.val : null));
		tNode = Solution.getNextNodeInOrder(root.right.right);
		System.out.println("RR<6>:"+(tNode != null ? tNode.val : null));
		tNode = Solution.getNextNodeInOrder(root.right.left.left);
		System.out.println("RR<8>:"+(tNode != null ? tNode.val : null));
		
	}
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值