1 /* 2 public class TreeLinkNode { 3 int val; 4 TreeLinkNode left = null; 5 TreeLinkNode right = null; 6 TreeLinkNode next = null;//指向父节点 7 8 TreeLinkNode(int val) { 9 this.val = val; 10 } 11 } 12 */ 13 public class Solution { 14 public TreeLinkNode GetNext(TreeLinkNode pNode) 15 { 16 if(pNode==null)return null; 17 if(pNode.right!=null){//如果有右子树则找右子树的最左边的节点 18 pNode = pNode.right; 19 while(pNode.left!=null) pNode = pNode.left; 20 return pNode; 21 } 22 23 while(pNode.next!=null){//如果没有右子树,则找第一个当前节点是父节点的左孩子的节点 24 if(pNode.next.left==pNode) return pNode.next; 25 pNode = pNode.next; 26 } 27 return null;//退到了根节点仍没有找到则返回null 28 } 29 }
原文:https://www.cnblogs.com/blzm742624643/p/12509112.html