二叉树的下一结点
二叉树的下一个结点
http://www.nowcoder.com/questionTerminal/9023a0c988684a53960365b889ceaf5e
解决思路,有右节点向下找,无右节点向上找。
虽说代码过了,但是牛客的测试用力可能不全,没继续测试。
public class Solution {
public TreeLinkNode GetNext(TreeLinkNode pNode) {
if (pNode == null) {
return null;
}
if (pNode.right != null) {
return getChilNode(pNode.right);
} else {
return getNextNode(pNode);
}
}
private TreeLinkNode getChilNode(TreeLinkNode pNode) {
if (pNode.left != null) {
return getChilNode(pNode.left);
}
return pNode;
}
private TreeLinkNode getNextNode(TreeLinkNode pNode) {
if (pNode.next != null) {
if (pNode.next.left == pNode) {
return pNode.next;
}
if (pNode.next.right == pNode) {
return getNextNode(pNode.next);
}
}
return null;
}
}