中序遍历二叉树的下一个节点

题目:给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。

 1 public class Solution {
 2     public TreeLinkNode GetNext(TreeLinkNode pNode)
 3     {
 4         if(pNode==null){
 5             return null;
 6         }
 7         
 8        //如果有右子树
 9         if(pNode.right!=null){
10             pNode=pNode.right;
11             while(pNode.left!=null){
12                 pNode=pNode.left;
13             }
14             return pNode;
15         }  
16         //如果没有右子树
17         while(pNode.next!=null){
18             if(pNode==pNode.next.left){
19                 return pNode.next;
20             }
21             pNode=pNode.next;
22         }
23         return null;
24     }
25 }

 

 

posted @ 2019-05-14 14:40  JingMo  阅读(153)  评论(0)    收藏  举报