您的位置:首页 > 其它

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

2014-08-03 19:16 344 查看
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));

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐