176 在二叉树中找到一个节点的后继节点
Table of Contents

ac

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

class TreeNode{
    TreeNode left;
    TreeNode right;
    int val;

    public TreeNode(int val) {
        this.val = val;
        left = null;
        right = null;
    }
}

public class Main {
    public static Scanner sc = new Scanner(System.in);

    public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    public static long mod = (long) Math.pow(2, 29);

    public static void main(String[] args) throws Exception {
        String line;
        String[] strArr;
        line = reader.readLine();
        strArr = line.split(" ");
        int n = Integer.valueOf(strArr[0]);
        int rootVal = Integer.valueOf(strArr[1]);
        Map<Integer, TreeNode> mp = new HashMap<>(n);
        for (int i = 0; i < n; i++) {
            line = reader.readLine();
            strArr = line.split(" ");
            int val = Integer.valueOf(strArr[0]);
            int leftVal = Integer.valueOf(strArr[1]);
            int rightVal = Integer.valueOf(strArr[2]);

            if (!mp.containsKey(val)) {
                mp.put(val, new TreeNode(val));
            }
            if (leftVal != 0) {
                if (!mp.containsKey(leftVal)) {
                    mp.put(leftVal, new TreeNode(leftVal));
                }
                mp.get(val).left = mp.get(leftVal);
            }
            if (rightVal != 0) {
                if (!mp.containsKey(rightVal)) {
                    mp.put(rightVal, new TreeNode(rightVal));
                }
                mp.get(val).right = mp.get(rightVal);
            }
        }
        int findVal = Integer.valueOf(reader.readLine());
        TreeNode root = mp.get(rootVal);
        System.out.println(findNode(root, findVal));
    }

    // 左 根 右
    static Integer findNode(TreeNode root, Integer findVal){
        if(root == null){
            return 0;
        }
        Integer nodeNext = null;
        boolean flag = false;
        TreeNode cur = root;
        while (cur != null){
            if(cur.left == null){
                // 打印
                if(flag && nodeNext == null){
                    nodeNext = cur.val;
                }
                if(cur.val == findVal){
                    flag = true;
                }
//                System.out.print(" " + cur.val);
                cur = cur.right;
            }else {
                TreeNode pre = cur.left;
                while (pre.right != null && pre.right != cur){
                    pre = pre.right;
                }
                if(pre.right == null){ // 第一次访问
                    // 建立联系,并转向左节点
                    pre.right = cur;
                    cur = cur.left;
                }else { // 第二次访问,表示cur的左子树都访问过了
                    // 打印
                    if(flag && nodeNext == null){
                        nodeNext = cur.val;
                    }
                    if(cur.val == findVal){
                        flag = true;
                    }
//                    System.out.print(" " + cur.val);
                    pre.right = null;
                    cur = cur.right;
                }
            }
        }
        return nodeNext == null ? 0 : nodeNext;
    }

}
/*
         6
      /     \
     3       9
    / \      / \
   1  4     8  10
    \  \   /
     2  5 7

10 6
6 3 9
3 1 4
1 0 2
2 0 0
4 0 5
5 0 0
9 8 10
10 0 0
8 7 0
7 0 0
10

0
 */