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。
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)); } }
相关文章推荐
- Cracking coding interview(2.2)返回单向链表的倒数第n个节点
- Cracking coding interview(4.1)二叉树平衡问题
- 剑指offer给定二叉树,求中序遍历的下一个 节点,前序遍历,后序遍历扩展
- 判断一个二叉树是否是平衡二叉树 Cracking the coding interview 4.1
- 找二叉树中指定节点在中序遍历中的下一个节点
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
- 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回
- Cracking the Coding Interview:: 寻找有环链表的环路起始节点
- 58 - 二叉树的中序遍历的下一个节点
- 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
- Cracking the Coding Interview:: 寻找有环链表的环路起始节点
- Cracking coding interview(4.3)给定一个排好序的数组创建最小高度的二叉树
- Cracking coding interview(4.4) 二叉树水平遍历
- 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
- 《程序员面试金典》--寻找二叉树中某个节点中序遍历的下一个节点
- 试编写一个函数,返回一颗给定二叉树在中序遍历下的最后一个节点(分别用递归和非递归实现)
- 返回中序遍历的一个节点的下一个节点
- 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回
- 二叉树是笔试面试中考试最频繁的数据结构之一,主要包括,程序建立一个二叉树,三种次序遍历二叉树,返回叶子节点的数目,求二叉树节点的总数等。建立一个二叉树节点的数据结构
- 二叉树 给定一个节点,求中序遍历下一个节点