首页 > 其他 > 详细

alicode35

时间:2020-03-05 09:55:37      阅读:57      评论:0      收藏:0      [点我收藏+]
 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 }

算法思路:二叉树遍历(中序遍历)。

二叉搜索树的中序遍历是有序的。

alicode35

原文:https://www.cnblogs.com/asenyang/p/12418159.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!