时间复杂度O(n)
后序遍历方式的递归处理,注意是子树,而不是子结构
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.Reader; import java.util.*; class TreeNode{ TreeNode left; TreeNode right; int val; public TreeNode(int val) { this.val = val; left = null; right = null; } } // 二叉搜索树的记录 class Record{ int bRootVal; int bMinVal; int bMaxVal; int bSize; public Record(int bRootVal, int bMinVal, int bMaxVal, int bSize) { this.bRootVal = bRootVal; this.bMinVal = bMinVal; this.bMaxVal = bMaxVal; this.bSize = bSize; } public Record() { this.bRootVal = -1; this.bMinVal = Integer.MAX_VALUE; this.bMaxVal = Integer.MIN_VALUE; this.bSize = 0; } } 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); } } TreeNode root = mp.get(rootVal); // System.out.println(getLen(root, k)); Record record = postOrder(root); System.out.println(record.bSize); } // 后序遍历递归 // 记录一个二叉搜索树的同节点,最小节点,最大节点 // 子树 和 子结构 static Record postOrder(TreeNode root){ if(root == null){ // 方便处理,这里不返回 null return new Record(); } if(root.left == null && root.right == null){ return new Record(root.val, root.val, root.val,1); } Record leftRecord = postOrder(root.left); Record rightRecord = postOrder(root.right); if(root.left != null && root.left.val == leftRecord.bRootVal && root.right != null && root.right.val == rightRecord.bRootVal && root.val > leftRecord.bMaxVal && root.val < rightRecord.bMinVal){ return new Record( root.val, leftRecord.bMinVal, rightRecord.bMaxVal, 1 + leftRecord.bSize + rightRecord.bSize ); } if(root.left != null && root.left.val == leftRecord.bRootVal && root.right == null && root.val > leftRecord.bMaxVal){ return new Record( root.val, leftRecord.bMinVal, root.val, 1 + leftRecord.bSize ); } if(root.right != null && root.right.val == rightRecord.bRootVal && root.left == null && root.val < rightRecord.bMinVal){ return new Record( root.val, root.val, rightRecord.bMaxVal, 1 + rightRecord.bSize ); } return leftRecord.bSize > rightRecord.bSize ? leftRecord : rightRecord; } } /* 3 2 2 1 3 1 0 0 3 0 0 3 7 / \ 1 12 / \ / \ 2 3 10 13 / \ / \ 5 14 20 16 / \ / \ 4 6 11 15 15 7 7 1 12 1 2 3 2 0 0 3 0 0 12 10 13 10 5 14 13 20 16 5 4 6 14 11 15 2 0 0 4 0 0 6 0 0 11 0 0 15 0 0 20 0 0 16 0 0 7 */