题解 | 剑指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);
        }
    }
}

全部评论

相关推荐

01-09 17:12
四川大学 Java
叁六玖:上次建行给我开25万,让我扣2办理
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务