1 package solution35;
2
3 import java.util.LinkedList;
4
5 public class Solution {
6 public LinkedList<Integer> list = new LinkedList<Integer>();
7 public void inOrder(TreeNode root){
8 if(root != null){
9 if(root.left != null){
10 inOrder(root.left);
11 }
12 this.list.add(root.val);
13 if(root.right != null){
14 inOrder(root.right);
15 }
16 }
17
18 }
19 public int solution(TreeNode root) {
20 this.inOrder(root);
21 int n = list.size();
22 return list.get(n-2);
23 }
24 }
算法思路:二叉树遍历(中序遍历)。
二叉搜索树的中序遍历是有序的。
来源:https://www.cnblogs.com/asenyang/p/12418159.html