二叉树的下一个结点
二叉树的下一个结点
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;
}
// 若父结点非空,则返回父结点作为左子树的对应父结点
while (pNode.next != null) {
if (pNode.next.left != pNode) {
pNode = pNode.next;
} else {
return pNode.next;
}
}
return null;
}
}
查看12道真题和解析
