题解 | 剑指offer | #二叉树的下一个结点#
二叉树的下一个结点
https://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e
import java.util.*;
/*
public class TreeLinkNode {
int val;
TreeLinkNode left = null;
TreeLinkNode right = null;
TreeLinkNode next = null;
TreeLinkNode(int val) {
this.val = val;
}
}
*/
public class Solution {
ArrayList<TreeLinkNode> inorder_array = new ArrayList<>();
public TreeLinkNode GetNext(TreeLinkNode pNode) {
TreeLinkNode root = pNode;
// if(root == null) return null;
while(root.next != null) root = root.next;
inorder(root);
for(int i = 0; i < inorder_array.size() - 1; i ++){
if(inorder_array.get(i) == pNode) return inorder_array.get(i + 1);
}
return null;
}
void inorder(TreeLinkNode root){
if(root != null){
inorder(root.left);
inorder_array.add(root);
inorder(root.right);
}
}
}
