剑指offer-57-二叉树的下一个结点
二叉树的下一个结点
http://www.nowcoder.com/questionTerminal/9023a0c988684a53960365b889ceaf5e
思路
分情况,总体上存在右子节点与没有,没有右子节点,分多种。
代码
public class Solution { public TreeLinkNode GetNext(TreeLinkNode pNode){ if(pNode==null){return null;} if(pNode.right!=null){ pNode=pNode.right; while(pNode.left!=null){ pNode=pNode.left; } return pNode; }else{//没有右子节点 if(pNode.next==null){return null;} if(pNode==pNode.next.left){ return pNode.next; }else{ if(pNode.next.next==null || pNode.next==pNode.next.next.right){ return null; }else{ return pNode.next.next; } } } } }
剑指offer与数据结构 文章被收录于专栏
本专栏包括剑指offer题目和一些刷题用的数据结构,单调栈,树状数组,差分数组,后面还会更新红黑树等较为复杂的数据结构