alicode35

时间:2020-03-05 09:55:37   收藏:0   阅读:57
 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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!